-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNodeAtDistanceKFromLeaf.java
49 lines (43 loc) · 1.69 KB
/
NodeAtDistanceKFromLeaf.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
package main.java.topcodingquestion.treesandgraphs;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class NodeAtDistanceKFromLeaf {
public static void main(String[] args) {
NodeAtDistanceKFromLeaf nodeAtDistanceKFromLeaf = new NodeAtDistanceKFromLeaf();
nodeAtDistanceKFromLeaf.leafNodeDistance(TreeNode.getSampleTree(), 1);
}
public void leafNodeDistance(TreeNode node, int dist) {
// list to store root-to-leaf path
List<TreeNode> path = new ArrayList<>();
// create an empty set to store distinct nodes at a given
// distance from leaf nodes
Set<TreeNode> set = new HashSet<>();
leafNodeDistance(node, path, set, dist);
for (TreeNode t : set)
System.out.println(t.val);
}
private void leafNodeDistance(TreeNode node, List<TreeNode> path, Set<TreeNode> set,
int dist) {
if (node == null) {
return;
}
// if a leaf node is found, insert the node at a distance `dist` from the
// leaf node into the set
if (isLeaf(node) && path.size() >= dist) {
set.add(path.get(path.size() - dist));
return;
}
// include the current node in the current path
path.add(node);
// recur for the left and right subtree
leafNodeDistance(node.left, path, set, dist);
leafNodeDistance(node.right, path, set, dist);
// remove the current node from the current path
path.remove(node);
}
private boolean isLeaf(TreeNode treeNode) {
return treeNode.left == null && treeNode.right == null;
}
}