-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem1609.cpp
53 lines (51 loc) · 1.92 KB
/
problem1609.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
45
46
47
48
49
50
51
52
53
/**
* Definition for a binary tree node.
* 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) {}
* };
*/
class Solution {
public:
bool isEvenOddTree(TreeNode* root) {
queue<TreeNode*>q;
TreeNode* current = root;
q.push(current);
if(root->val%2 == 0)return false;
int level = 1;
while(q.size() != 0){
if(level >= 4){
cout << q.size() << endl;
}
deque<long>v;
int size = q.size();
while(size--){
current = q.front();
q.pop();
if(current->left != nullptr)
q.push(current->left), v.push_back(current->left->val);
if(current->right != nullptr)
q.push(current->right), v.push_back(current->right->val);
}
// fuckkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
if(v.empty())break;
if(level%2){
if(v[0]%2)return false;
for(int i = 1;i < v.size(); i++)
if(v[i] >= v[i-1] || v[i]%2)
return false;
}else{
if(v[0]%2 == 0)return false;
for(int i = 1;i < v.size(); i++)
if(v[i] <= v[i-1] || v[i]%2 == 0)
return false;
}
level++;
}
return true;
}
};