-
Notifications
You must be signed in to change notification settings - Fork 0
/
CousinsInBinaryTree.java
83 lines (75 loc) · 2.3 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
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
79
80
81
82
83
// https://leetcode.com/problems/cousins-in-binary-tree/
// #tree #bfs
/**
* 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) {
Deque<TreeNode> q = new LinkedList<>();
q.addLast(root);
while (!q.isEmpty()) {
int l = q.size(); // get number of node in depth k
int[] found = new int[2]; // 0 - found, 1 - foundParent / 1:x, 2:y, 3:x+y
for (int i = 0; i < l; i++) {
TreeNode u = q.pollFirst();
found[1] = 0;
checkFound(u.left, x, y, found);
checkFound(u.right, x, y, found);
if (found[1] == 3) // same parent
return false;
if (u.left != null) q.addLast(u.left);
if (u.right != null) q.addLast(u.right);
}
if (found[0] == 3) // found x, y with different parent
return true;
}
return false;
}
private void checkFound(TreeNode node, int x, int y, int[] found) {
if (node == null) return;
if (node.val == x) {
found[0] |= 1;
found[1] |= 1;
}
if (node.val == y) {
found[0] |= 2;
found[1] |= 2;
}
}
/*
public boolean isCousins(TreeNode root, int x, int y) {
int[] depth = new int[2];
depth[0] = -1; // x;
depth[1] = -2; // y;
TreeNode[] parent = new TreeNode[2];
return (findDepth(root, parent, null, x, y, depth, 0) == 1);
}
private int findDepth(TreeNode root, TreeNode[] parent, TreeNode par,
int x, int y, int[] depth, int curDepth) {
if (root == null) return 0;
if (root.val == x) {
parent[0] = par;
depth[0] = curDepth;
}
if (root.val == y) {
parent[1] = par;
depth[1] = curDepth;
}
if (depth[0] == depth[1]){
if (parent[0] == parent[1]) {
return -1;
}
else {
return 1;
}
}
int ret = findDepth(root.left, parent, root, x, y, depth, curDepth + 1);
if (ret == 0) {
ret = findDepth(root.right, parent, root, x, y, depth, curDepth + 1);
}
return ret;
}
*/
}