leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1609.cpp (884B)
0 class Solution {
1 public:
2 bool isEvenOddTree(TreeNode *root) {
3 if (!root) return false;
5 queue<TreeNode *> q;
6 TreeNode *de = new TreeNode(0);
7 TreeNode *dd = new TreeNode(INT_MAX);
9 q.push(root);
10 for (int lvl = 0; !q.empty(); lvl++) {
11 TreeNode *p = (lvl % 2 == 0) ? de : dd;
12 for (int t = q.size(); t > 0; t--) {
13 TreeNode *root = q.front();
14 q.pop();
15 if (lvl % 2 == 0) {
16 if (root->val % 2 != 1 || root->val <= p->val) return false;
17 } else {
18 if (root->val % 2 != 0 || root->val >= p->val) return false;
19 }
20 p = root;
21 if (root->left) q.push(root->left);
22 if (root->right) q.push(root->right);
23 }
24 }
25 return true;
26 }
27 };