-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathEven Odd Tree.java
57 lines (48 loc) · 1.83 KB
/
Even Odd Tree.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
/*
Solution by Rahul Surana
***********************************************************
A binary tree is named Even-Odd if it meets the following conditions:
The root of the binary tree is at level index 0, its children are at level index 1, their children are at level index 2, etc.
For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).
Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.
***********************************************************
*/
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public boolean isEvenOddTree(TreeNode root) {
int mult = 1, oe = 1;
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
int size = q.size();
int prev = Integer.MIN_VALUE;
while(size-- > 0){
TreeNode n = q.poll();
if(n.val%2 != oe || (mult *n.val) <= (mult * prev)) return false;
// System.out.println((mult *n.val) +" <= "+(mult * prev.val));
prev = n.val;
if(n.left != null) q.add(n.left);
if(n.right != null) q.add(n.right);
}
mult *= -1;
oe = 1 - oe;
// System.out.println(mult +" "+oe);
}
return true;
}
}