-
Notifications
You must be signed in to change notification settings - Fork 0
/
CousinsBinaryTree.java
68 lines (60 loc) · 1.73 KB
/
CousinsBinaryTree.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
/**
* 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) {
int level = -1, Lx=0,Ly=0,Px=0,Py=0;
Queue<TreeNode> que= new LinkedList<>();
List<TreeNode> removedNode = new ArrayList<>();
que.add(root);
que.add(null);
while(que.size()>0){
TreeNode node= que.peek();
que.remove();
if(node == null){
level++;
if(que.size()!=0)
que.add(null);
}
else{
if(node.val==x){
Lx=level+1;
Px= findParent(removedNode,x);
}
else if(node.val==y){
Ly=level+1;
Py= findParent(removedNode,y);
}
if(node.left!=null)
que.add(node.left);
if(node.right!=null)
que.add(node.right);
removedNode.add(node);
}
}
if(Lx==Ly && Px!=Py)
return true;
else
return false;
}
public int findParent(List<TreeNode> list, int n){
for(int i = list.size()-1;i>=0;i--){
TreeNode nod= list.get(i);
if((nod.left!=null && nod.left.val==n) || (nod.right!=null && nod.right.val==n))
return nod.val;
}
return -1;
}
}