Skip to content

Commit b779a5c

Browse files
author
Sherali
committed
today's contribution, medium problem, k distance tree nodes
1 parent 29ea919 commit b779a5c

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎src/problems/Medium.txt‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2858,3 +2858,33 @@ Note:
28582858
0 < speed[i] <= 10 ^ 6
28592859
0 <= position[i] < target
28602860
All initial positions are different.
2861+
2862+
192)All Nodes Distance K in Binary Tree
2863+
We are given a binary tree (with root node root), a target node, and an integer value K.
2864+
2865+
Return a list of the values of all nodes that have a distance K from the target node. The answer can be returned in any order.
2866+
2867+
2868+
2869+
Example 1:
2870+
2871+
Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2
2872+
2873+
Output: [7,4,1]
2874+
2875+
Explanation:
2876+
The nodes that are a distance 2 from the target node (with value 5)
2877+
have values 7, 4, and 1.
2878+
2879+
2880+
2881+
Note that the inputs "root" and "target" are actually TreeNodes.
2882+
The descriptions of the inputs above are just serializations of these objects.
2883+
2884+
2885+
Note:
2886+
2887+
The given tree is non-empty.
2888+
Each node in the tree has unique values 0 <= node.val <= 500.
2889+
The target node is a node in the tree.
2890+
0 <= K <= 1000.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package problems.medium;
2+
3+
import problems.utils.TreeNode;
4+
5+
import java.util.*;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* Why Did you create this class? what does it do?
10+
*/
11+
public class AllNodesDistanceKinBinaryTree {
12+
public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
13+
List<Integer> list = new ArrayList<>();
14+
if (root == null || target == null || k < 0)
15+
return list;
16+
if (k == 0) {
17+
list.add(target.val);
18+
return list;
19+
}
20+
Map<TreeNode, Set<TreeNode>> map = new HashMap<>();
21+
map.put(root, new HashSet<>());
22+
23+
map.get(root).add(root.left);
24+
map.get(root).add(root.right);
25+
26+
go(map, root.left, root);
27+
go(map, root.right, root);
28+
29+
return bfs(map, target, k);
30+
}
31+
32+
List<Integer> bfs(Map<TreeNode, Set<TreeNode>> map, TreeNode src, int k) {
33+
Queue<TreeNode> q = new LinkedList<>();
34+
q.add(src);
35+
Set<TreeNode> visited = new HashSet<>();
36+
int count = 1;
37+
while (!q.isEmpty()) {
38+
TreeNode current = q.remove();
39+
count--;
40+
//System.out.println(current.val);
41+
visited.add(current);
42+
if (map.get(current) != null)
43+
for (TreeNode nei : map.get(current)) {
44+
if (!visited.contains(nei)) {
45+
q.add(nei);
46+
}
47+
}
48+
if (count == 0) {
49+
k--;
50+
count += q.size();
51+
}
52+
if (k == 0) {
53+
return q.stream().filter(e -> e != null).map(e -> e.val).collect(Collectors.toList());
54+
}
55+
}
56+
return new ArrayList<>();
57+
}
58+
59+
void go(Map<TreeNode, Set<TreeNode>> map, TreeNode x, TreeNode parent) {
60+
if (x == null)
61+
return;
62+
map.put(x, new HashSet<>());
63+
map.get(x).add(parent);
64+
if (x.left != null)
65+
map.get(x).add(x.left);
66+
if (x.right != null)
67+
map.get(x).add(x.right);
68+
go(map, x.left, x);
69+
go(map, x.right, x);
70+
71+
}
72+
}

0 commit comments

Comments
 (0)