diff options
Diffstat (limited to 'store/works/solutions/leetcode/cpp/1609.cpp')
-rw-r--r-- | store/works/solutions/leetcode/cpp/1609.cpp | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/store/works/solutions/leetcode/cpp/1609.cpp b/store/works/solutions/leetcode/cpp/1609.cpp new file mode 100644 index 0000000..e446c0f --- /dev/null +++ b/store/works/solutions/leetcode/cpp/1609.cpp @@ -0,0 +1,94 @@ +struct TreeNode
+{
+ int val;
+ TreeNode *left;
+ TreeNode *right;
+ TreeNode() : val(0), left(nullptr), right(nullptr) {}
+ TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
+ TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
+};
+
+#include <queue>
+
+class Solution
+{
+public:
+ bool isEvenOddTree(TreeNode *root)
+ {
+ if (root == nullptr)
+ return true;
+ std::queue<TreeNode *> odd_level;
+ std::queue<TreeNode *> even_level;
+
+ even_level.push(root);
+
+ while (true)
+ {
+ if (!odd_level.empty())
+ {
+ auto node = odd_level.front();
+ odd_level.pop();
+ if (node->left)
+ even_level.push(node->left);
+ if (node->right)
+ even_level.push(node->right);
+ int last = node->val;
+ if (last % 2 != 0)
+ return false;
+
+ while (!odd_level.empty())
+ {
+ node = odd_level.front();
+ odd_level.pop();
+ if (node->left)
+ even_level.push(node->left);
+ if (node->right)
+ even_level.push(node->right);
+ int val = node->val;
+ if (val % 2 != 0)
+ return false;
+ if (val >= last)
+ return false;
+ last = val;
+ }
+
+ continue;
+ }
+
+ if (!even_level.empty())
+ {
+ auto node = even_level.front();
+ even_level.pop();
+ if (node->left)
+ odd_level.push(node->left);
+ if (node->right)
+ odd_level.push(node->right);
+ int last = node->val;
+ if (last % 2 == 0)
+ return false;
+
+ while (!even_level.empty())
+ {
+ node = even_level.front();
+ even_level.pop();
+ if (node->left)
+ odd_level.push(node->left);
+ if (node->right)
+ odd_level.push(node->right);
+ int val = node->val;
+ if (val % 2 == 0)
+ return false;
+ if (val <= last)
+ return false;
+ last = val;
+ }
+
+ continue;
+ }
+
+ break;
+ }
+
+ return true;
+ }
+};
\ No newline at end of file |