-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCousinsInBinaryTree.java
44 lines (35 loc) · 1.11 KB
/
CousinsInBinaryTree.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
package com.company;
public class CousinsInBinaryTree {
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;
}
}
int depth = -1, parent = -1; // mark the first target hit during search
public boolean isCousins(TreeNode root, int x, int y) {
return dfs(root, x, y, -2, 0);
}
boolean dfs(TreeNode node, int x, int y, int p, int d) {
if (node == null) return true;
if (node.val == x || node.val == y) {
if (depth != -1 && parent != -1) { // if the other target already hit previously
if (depth == d && parent != p) return true;
else return false;
} else {
depth = d;
parent = p;
}
}
return dfs(node.left, x, y, node.val, d + 1) && dfs(node.right, x, y, node.val, d + 1);
}
}