-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathCousins in Binary Tree.java
78 lines (62 loc) · 2.38 KB
/
Cousins in Binary 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Runtime: 0 ms (Top 100.0%) | Memory: 41.30 MB (Top 42.36%)
/**
* 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 isCousins(TreeNode root, int x, int y) {
//If any of x or y is at root, it means they can't be at same depth. Return false.
if(root.val == x || root.val == y) return false;
Deque<TreeNode> queue = new LinkedList<>();
queue.offer(root);
boolean xFound = false;
boolean yFound = false;
int parentX = 0;
int parentY = 0;
//Do level-order traversal until x or y is found or queue is empty.
while(!queue.isEmpty() && !xFound && !yFound){
int size = queue.size();
//Traverse that level.
while(size-- > 0){
TreeNode node = queue.poll();
//if x or y is found at left/right, save the parent and set the "found" flag to true.
//This flag will break the loop as soon as any one (x or y) is found.
//we don't need to go deeper to find second if it isn't found at this level.
if(node.left != null) {
queue.offer(node.left);
if(node.left.val == x){
parentX = node.val;
xFound = true;
}
if(node.left.val == y){
parentY = node.val;
yFound = true;
}
}
if(node.right != null) {
queue.offer(node.right);
if(node.right.val == x){
parentX = node.val;
xFound = true;
}
if(node.right.val == y){
parentY = node.val;
yFound = true;
}
}
}
}
return xFound && yFound && parentX != parentY;
}
}