-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay7-CousinInBinaryString.java
40 lines (37 loc) · 1.14 KB
/
Day7-CousinInBinaryString.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
/**
* 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) {
Pair pair1=cousinHelper(root,x,0,root);
Pair pair2=cousinHelper(root,y,0,root);
return pair1.depth==pair2.depth && pair1.parent!=pair2.parent
}
public Pair cousinHelper(TreeNode node, int val,int level, TreeNode parent){
if(node==null){ return null; }
if(node.val==val){ return new Pair(parent,level); }
Pair leftpair=cousinHelper(node.left,val,level+1,node);
if(leftpair!=null) return leftpair;
return cousinHelper(node.right,val,level+1,node);
}
}
class Pair{
TreeNode parent;
int depth;
Pair(TreeNode parent, int depth){
this.parent=parent;
this.depth=depth;
}
}