-
Notifications
You must be signed in to change notification settings - Fork 0
/
even_odd_tree.cpp
37 lines (32 loc) · 1023 Bytes
/
even_odd_tree.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
35
36
37
class Solution {
public:
bool isEvenOddTree(TreeNode* root) {
queue<TreeNode*> myQueue;
TreeNode* cur = root;
myQueue.push(cur);
int index = 0;
while(myQueue.size()!=0){
int size = myQueue.size();
int pre = -1;
for(int i=0;i<size;i++){
TreeNode* node = myQueue.front();
if(index%2==0 && (node->val%2==0 || (i>0 && node->val<=pre))){
return false;
}
if(index%2==1 && (node->val%2==1 || (i>0 && node->val>=pre))){
return false;
}
pre = node->val;
myQueue.pop();
if(node->left!=nullptr){
myQueue.push(node->left);
}
if(node->right!=nullptr){
myQueue.push(node->right);
}
}
index++;
}
return true;
}
};