-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvenOddTree_1609.java
65 lines (61 loc) · 2.04 KB
/
EvenOddTree_1609.java
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
54
55
56
57
58
59
60
61
62
63
64
package org.example;
import java.util.ArrayList;
public class EvenOddTree_1609 {
class Solution {
public boolean isEvenOddTree(TreeNode root) {
ArrayList<TreeNode> left = new ArrayList<>();
ArrayList<TreeNode> right = new ArrayList<>();
int round = 0;
left.add(root);
while (left.size()>0 || right.size()>0){
if (round==0){
int cur = 0;
while (left.size()>0){
TreeNode treeNode = left.remove(0);
if (treeNode.val%2==0 || treeNode.val<=cur) {
return false;
}
cur = treeNode.val;
if (treeNode.left != null){
right.add(treeNode.left);
}
if (treeNode.right != null){
right.add(treeNode.right);
}
}
round = 1;
}
else {
int cur = 1000001;
while (right.size()>0){
TreeNode treeNode = right.remove(0);
if (treeNode.val%2==1 || treeNode.val>=cur) {
return false;
}
cur = treeNode.val;
if (treeNode.left != null){
left.add(treeNode.left);
}
if (treeNode.right != null){
left.add(treeNode.right);
}
}
round = 0;
}
}
return true;
}
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}