generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
34 lines (29 loc) · 805 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <list>
#include <utility>
class Solution {
public:
bool isEvenOddTree(TreeNode* root) {
std::list<TreeNode*> nodes{root};
std::list<TreeNode*> prevNodes;
bool isOdd = true;
while (!nodes.empty()) {
std::swap(prevNodes, nodes);
nodes.clear();
int prevVal = isOdd ? 0 : 1000001;
for (const auto node : prevNodes) {
if (isOdd) {
if (node->val <= prevVal || node->val % 2 != 1)
return false;
} else {
if (node->val >= prevVal || node->val % 2 != 0)
return false;
}
prevVal = node->val;
if (node->left != nullptr) nodes.push_back(node->left);
if (node->right != nullptr) nodes.push_back(node->right);
}
isOdd = !isOdd;
}
return true;
}
};