-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAll Nodes Distance K in Binary Tree.java
45 lines (38 loc) · 1.3 KB
/
All Nodes Distance K in Binary Tree.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
class Solution {
public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
List<Integer> ans = new ArrayList<>();
Map<TreeNode, Integer> nodeToDist = new HashMap<>(); // {node: distance to target}
getDists(root, target, nodeToDist);
dfs(root, k, 0, nodeToDist, ans);
return ans;
}
private void getDists(TreeNode root, TreeNode target, Map<TreeNode, Integer> nodeToDist) {
if (root == null)
return;
if (root == target) {
nodeToDist.put(root, 0);
return;
}
getDists(root.left, target, nodeToDist);
if (nodeToDist.containsKey(root.left)) {
// The target is in the left subtree
nodeToDist.put(root, nodeToDist.get(root.left) + 1);
return;
}
getDists(root.right, target, nodeToDist);
if (nodeToDist.containsKey(root.right))
// The target is in the right subtree
nodeToDist.put(root, nodeToDist.get(root.right) + 1);
}
private void dfs(TreeNode root, int k, int dist, Map<TreeNode, Integer> nodeToDist,
List<Integer> ans) {
if (root == null)
return;
if (nodeToDist.containsKey(root))
dist = nodeToDist.get(root);
if (dist == k)
ans.add(root.val);
dfs(root.left, k, dist + 1, nodeToDist, ans);
dfs(root.right, k, dist + 1, nodeToDist, ans);
}
}