-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
AllNodesDistanceKinBinaryTree.java
72 lines (63 loc) · 2.05 KB
/
AllNodesDistanceKinBinaryTree.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
package problems.medium;
import problems.utils.TreeNode;
import java.util.*;
import java.util.stream.Collectors;
/**
* Why Did you create this class? what does it do?
*/
public class AllNodesDistanceKinBinaryTree {
public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
List<Integer> list = new ArrayList<>();
if (root == null || target == null || k < 0)
return list;
if (k == 0) {
list.add(target.val);
return list;
}
Map<TreeNode, Set<TreeNode>> map = new HashMap<>();
map.put(root, new HashSet<>());
map.get(root).add(root.left);
map.get(root).add(root.right);
go(map, root.left, root);
go(map, root.right, root);
return bfs(map, target, k);
}
List<Integer> bfs(Map<TreeNode, Set<TreeNode>> map, TreeNode src, int k) {
Queue<TreeNode> q = new LinkedList<>();
q.add(src);
Set<TreeNode> visited = new HashSet<>();
int count = 1;
while (!q.isEmpty()) {
TreeNode current = q.remove();
count--;
//System.out.println(current.val);
visited.add(current);
if (map.get(current) != null)
for (TreeNode nei : map.get(current)) {
if (!visited.contains(nei)) {
q.add(nei);
}
}
if (count == 0) {
k--;
count += q.size();
}
if (k == 0) {
return q.stream().filter(e -> e != null).map(e -> e.val).collect(Collectors.toList());
}
}
return new ArrayList<>();
}
void go(Map<TreeNode, Set<TreeNode>> map, TreeNode x, TreeNode parent) {
if (x == null)
return;
map.put(x, new HashSet<>());
map.get(x).add(parent);
if (x.left != null)
map.get(x).add(x.left);
if (x.right != null)
map.get(x).add(x.right);
go(map, x.left, x);
go(map, x.right, x);
}
}