-
Notifications
You must be signed in to change notification settings - Fork 0
/
CousinBT.java
51 lines (44 loc) · 1.05 KB
/
CousinBT.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
class Solution {
public boolean isCousins(TreeNode root, int x, int y) {
TreeNode xx = findNode(root, x);
TreeNode yy = findNode(root, y);
return (
(level(root, xx, 0) == level(root, yy, 0)) && (!isSibling(root, xx, yy))
);
}
TreeNode findNode(TreeNode node, int x) {
if (node == null) {
return null;
}
if (node.val == x) {
return node;
}
TreeNode n = findNode(node.left, x);
if (n != null) {
return n;
}
return findNode(node.right, x);
}
boolean isSibling (TreeNode node, TreeNode x, TreeNode y) {
if (node == null) {
return false;
}
return (
(node.left == x && node.right == y) || (node.left == y && node.right == x)
|| isSibling(node.left, x, y) || isSibling(node.right, x, y)
);
}
int level (TreeNode node, TreeNode x, int lev) {
if(node == null) {
return 0;
}
if(node == x) {
return lev;
}
int l = level(node.left, x, lev+1);
if (l != 0) {
return l;
}
return level(node.right, x, lev+1);
}
}