-
Notifications
You must be signed in to change notification settings - Fork 0
/
Even Odd Tree.cpp
44 lines (44 loc) · 1.09 KB
/
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
38
39
40
41
42
43
44
int solve(TreeNode* root){
if(root==NULL){
return 1;
}
queue<TreeNode*> q;
q.push(root);
int level=0;
int prev;
while(!q.empty()){
int size=q.size();
if(level%2==0){
prev=0;
for(int i=0;i<size;i++){
auto el = q.front();
q.pop();
if(el->val>prev && el->val%2!=0){
prev=el->val;
}
else{
return 0;
}
if(el -> left) q.push(el -> left);
if(el -> right) q.push(el -> right);
}
}
else{
prev=1000001;
for(int i=0;i<size;i++){
auto el = q.front();
q.pop();
if(el->val<prev && el->val%2==0){
prev=el->val;
}
else{
return 0;
}
if(el -> left) q.push(el -> left);
if(el -> right) q.push(el -> right);
}
}
++level;
}
return 1;
}