forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_1609.java
53 lines (50 loc) · 1.77 KB
/
_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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class _1609 {
public static class Solution1 {
public boolean isEvenOddTree(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int level = 0;
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
list.add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
if (level % 2 == 0) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i) % 2 == 0) {
return false;
}
if (i < list.size() - 1 && list.get(i) >= list.get(i + 1)) {
return false;
}
}
} else {
for (int i = 0; i < list.size(); i++) {
if (list.get(i) % 2 != 0) {
return false;
}
if (i < list.size() - 1 && list.get(i) <= list.get(i + 1)) {
return false;
}
}
}
level++;
}
return true;
}
}
}