Skip to content

Commit f782398

Browse files
add 863
1 parent 2cb827f commit f782398

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ _If you like this project, please leave me a star._ ★
704704
| 870 | [Advantage Shuffle](https://leetcode.com/problems/advantage-shuffle/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_870.java) | | Medium | Array, Greedy
705705
| 868 | [Binary Gap](https://leetcode.com/problems/binary-gap/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_868.java) | | Easy |
706706
| 867 | [Transpose Matrix](https://leetcode.com/problems/transpose-matrix/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_867.java) | | Easy |
707+
| 863 | [All Nodes Distance K in Binary Tree](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_863.java) | | Medium | BFS
707708
| 861 | [Score After Flipping Matrix](https://leetcode.com/problems/score-after-flipping-matrix/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_861.java) | | Medium | Greedy
708709
| 860 | [Lemonade Change](https://leetcode.com/problems/lemonade-change/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_860.java) | | Easy |
709710
| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_859.java) | | Easy |

Diff for: src/main/java/com/fishercoder/solutions/_863.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.*;
6+
7+
public class _863 {
8+
public static class Solution1 {
9+
/**
10+
* Since it's asking for distance k, a.k.a shortest distance, BFS should be the way to go.
11+
* For this particular problem: we'll do BFS twice:
12+
* 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;
13+
* 2nd time: we push the target node into the queue, traverse all its neighbors (children and parent),
14+
* push them into the queue and decrement k by one, until k becomes zero, remaining elements in the queue are the answer.
15+
*/
16+
public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
17+
Map<Integer, TreeNode> childToParentMap = new HashMap<>();
18+
Queue<TreeNode> queue = new LinkedList<>();
19+
queue.offer(root);
20+
while (!queue.isEmpty()) {
21+
int size = queue.size();
22+
for (int i = 0; i < size; i++) {
23+
TreeNode curr = queue.poll();
24+
if (curr.left != null) {
25+
childToParentMap.put(curr.left.val, curr);
26+
queue.offer(curr.left);
27+
}
28+
if (curr.right != null) {
29+
childToParentMap.put(curr.right.val, curr);
30+
queue.offer(curr.right);
31+
}
32+
}
33+
}
34+
queue.offer(target);
35+
Set<Integer> visited = new HashSet<>();
36+
while (k > 0 && !queue.isEmpty()) {
37+
int size = queue.size();
38+
for (int i = 0; i < size; i++) {
39+
TreeNode curr = queue.poll();
40+
visited.add(curr.val);
41+
if (curr.left != null && !visited.contains(curr.left.val)) {
42+
queue.offer(curr.left);
43+
}
44+
if (curr.right != null && !visited.contains(curr.right.val)) {
45+
queue.offer(curr.right);
46+
}
47+
if (childToParentMap.containsKey(curr.val) && !visited.contains(childToParentMap.get(curr.val).val)) {
48+
queue.offer(childToParentMap.get(curr.val));
49+
}
50+
}
51+
k--;
52+
}
53+
List<Integer> list = new ArrayList<>();
54+
while (!queue.isEmpty()) {
55+
list.add(queue.poll().val);
56+
}
57+
return list;
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)