-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_863.java
60 lines (58 loc) · 2.63 KB
/
_863.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
package com.fishercoder.solutions.firstthousand;
import com.fishercoder.common.classes.TreeNode;
import java.util.*;
public class _863 {
public static class Solution1 {
/*
* Since it's asking for distance k, a.k.a shortest distance, BFS should be the way to go.
* For this particular problem: we'll do BFS twice:
* 1st time: we build a child to parent mapping, in binary tree, there's only parent to children mapping, so we'll need to establish this child to parent link;
* 2nd time: we push the target node into the queue, traverse all its neighbors (children and parent),
* push them into the queue and decrement k by one, until k becomes zero, remaining elements in the queue are the answer.
*/
public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
Map<Integer, TreeNode> childToParentMap = new HashMap<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode curr = queue.poll();
if (curr.left != null) {
childToParentMap.put(curr.left.val, curr);
queue.offer(curr.left);
}
if (curr.right != null) {
childToParentMap.put(curr.right.val, curr);
queue.offer(curr.right);
}
}
}
queue.offer(target);
Set<Integer> visited = new HashSet<>();
while (k > 0 && !queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode curr = queue.poll();
visited.add(curr.val);
if (curr.left != null && !visited.contains(curr.left.val)) {
queue.offer(curr.left);
}
if (curr.right != null && !visited.contains(curr.right.val)) {
queue.offer(curr.right);
}
if (childToParentMap.containsKey(curr.val)
&& !visited.contains(childToParentMap.get(curr.val).val)) {
queue.offer(childToParentMap.get(curr.val));
}
}
k--;
}
List<Integer> list = new ArrayList<>();
while (!queue.isEmpty()) {
list.add(queue.poll().val);
}
return list;
}
}
}