Skip to content

Commit 1b4142b

Browse files
Closest Leaf in a Binary Tree - Accepted
1 parent 449ff74 commit 1b4142b

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ My accepted leetcode solutions to some of the common interview problems.
309309
- [Minimum Absolute Difference in BST](problems/src/tree/MinimumAbsoluteDifferenceInBST.java) (Medium)
310310
- [Equal Tree Partition](problems/src/tree/EqualTreePartition.java) (Medium)
311311
- [Split BST](problems/src/tree/SplitBST.java) (Medium)
312+
- [Closest Leaf in a Binary Tree](problems/src/tree/ClosestLeafInABinaryTree.java) (Medium)
312313

313314
#### [Two Pointers](problems/src/two_pointers)
314315

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package tree;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 02/05/2018.
7+
* Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree.
8+
9+
Here, nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf of the tree. Also, a node is called a leaf if it has no children.
10+
11+
In the following examples, the input tree is represented in flattened form row by row. The actual root tree given will be a TreeNode object.
12+
13+
Example 1:
14+
15+
Input:
16+
root = [1, 3, 2], k = 1
17+
Diagram of binary tree:
18+
1
19+
/ \
20+
3 2
21+
22+
Output: 2 (or 3)
23+
24+
Explanation: Either 2 or 3 is the nearest leaf node to the target of 1.
25+
Example 2:
26+
27+
Input:
28+
root = [1], k = 1
29+
Output: 1
30+
31+
Explanation: The nearest leaf node is the root node itself.
32+
Example 3:
33+
34+
Input:
35+
root = [1,2,3,4,null,null,null,5,null,6], k = 2
36+
Diagram of binary tree:
37+
1
38+
/ \
39+
2 3
40+
/
41+
4
42+
/
43+
5
44+
/
45+
6
46+
47+
Output: 3
48+
Explanation: The leaf node with value 3 (and not the leaf node with value 6) is nearest to the node with value 2.
49+
Note:
50+
root represents a binary tree with at least 1 node and at most 1000 nodes.
51+
Every node has a unique node.val in range [1, 1000].
52+
There exists some node in the given binary tree for which node.val == k.
53+
54+
Solution: O(N): Maintain a hashmap of distances from each node in the first iteration. In the second iteration,
55+
find the key value node and then calculate distance from each node during backtrack.
56+
*/
57+
public class ClosestLeafInABinaryTree {
58+
59+
public static class TreeNode {
60+
int val;
61+
TreeNode left;
62+
TreeNode right;
63+
TreeNode(int x) { val = x; }
64+
}
65+
66+
private static class Pair{
67+
int n, d;
68+
Pair(int n, int d){
69+
this.n = n;
70+
this.d = d;
71+
}
72+
}
73+
74+
private Map<Integer, Pair> map;
75+
private Pair minNode;
76+
/**
77+
* Main method
78+
* @param args
79+
* @throws Exception
80+
*/
81+
public static void main(String[] args) throws Exception{
82+
TreeNode root = new TreeNode(1);
83+
root.left = new TreeNode(2);
84+
root.right = new TreeNode(3);
85+
root.left.left = new TreeNode(4);
86+
root.left.left.left = new TreeNode(5);
87+
root.left.left.left.left = new TreeNode(6);
88+
//root.right = new TreeNode(3);
89+
System.out.println(new ClosestLeafInABinaryTree().findClosestLeaf(root, 2));
90+
}
91+
92+
public int findClosestLeaf(TreeNode root, int k) {
93+
map = new HashMap<>();
94+
minNode = new Pair(-1, Integer.MAX_VALUE);
95+
findDistanceToLeaf(root);
96+
findMin(root, k);
97+
return minNode.n;
98+
}
99+
100+
private Pair findDistanceToLeaf(TreeNode node){
101+
if(node != null){
102+
if(node.left == null && node.right == null){
103+
map.put(node.val, new Pair(node.val, 0));
104+
return new Pair(node.val, 1);
105+
} else {
106+
Pair left = findDistanceToLeaf(node.left);
107+
Pair right = findDistanceToLeaf(node.right);
108+
if(left.d < right.d){
109+
map.put(node.val, left);
110+
return new Pair(left.n, left.d + 1);
111+
} else{
112+
map.put(node.val, right);
113+
return new Pair(right.n, right.d + 1);
114+
}
115+
}
116+
} return new Pair(-1, Integer.MAX_VALUE);
117+
}
118+
119+
private int findMin(TreeNode node, int k){
120+
if(node != null){
121+
if(node.val == k){
122+
if(map.get(node.val).d < minNode.d){
123+
minNode = map.get(node.val);
124+
}
125+
return 1;
126+
} else{
127+
int left = findMin(node.left, k);
128+
int right = findMin(node.right, k);
129+
if(left != -1){
130+
if((left + map.get(node.val).d) < minNode.d){
131+
minNode = new Pair(map.get(node.val).n, (left + map.get(node.val).d));
132+
}
133+
return left + 1;
134+
}
135+
else if(right != -1){
136+
if((right + map.get(node.val).d) < minNode.d){
137+
minNode = new Pair(map.get(node.val).n, (right + map.get(node.val).d));
138+
}
139+
return right + 1;
140+
}
141+
}
142+
}
143+
return -1;
144+
}
145+
}

0 commit comments

Comments
 (0)