https://github.com/emahtab/closest-leaf-in-a-binary-tree
Closest Leaf node from a given node in a binary tree
https://github.com/emahtab/closest-leaf-in-a-binary-tree
breadth-first-search closest-leaf graph leetcode
Last synced: 8 months ago
JSON representation
Closest Leaf node from a given node in a binary tree
- Host: GitHub
- URL: https://github.com/emahtab/closest-leaf-in-a-binary-tree
- Owner: eMahtab
- Created: 2021-10-02T06:06:51.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-31T05:13:39.000Z (about 3 years ago)
- Last Synced: 2025-02-02T03:26:11.961Z (10 months ago)
- Topics: breadth-first-search, closest-leaf, graph, leetcode
- Homepage:
- Size: 1.38 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Closest Leaf in a binary tree
Closest Leaf node from a given node in a binary tree
## https://leetcode.com/problems/closest-leaf-in-a-binary-tree
## https://www.lintcode.com/problem/854
Given the root of a binary tree where every node has a unique value and a target integer k, return the value of the nearest leaf node to the target k in the tree.
Nearest to a leaf means the least number of edges traveled on the binary tree to reach any leaf of the tree. Also, a node is called a leaf if it has no children.
```
Constraints:
1. The number of nodes in the tree is in the range [1, 1000].
2. 1 <= Node.val <= 1000
3. All the values of the tree are unique.
4. There exist some node in the tree where Node.val == k.
```
## Approach : Create graph from the given tree and BFS
We will first create a graph from the given binary tree. Once we have the graph, all we need to do is, to find the first leaf node using BFS, starting from the sourceNode `k`.
So this problem is, finding the shortest path in an unweighted undirected graph.

# Implementation : Graph + BFS
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
TreeNode sourceNode = null;
public int findClosestLeaf(TreeNode root, int k) {
if(root == null)
return -1;
Map> graph = new HashMap<>();
// Create a graph representation from given binary
dfs(root, graph, null, k);
// Next do BFS traversal to find the closest leaf to node k
Queue q = new ArrayDeque<>();
Set visited = new HashSet<>();
// Start the BFS from the sourceNode k
q.add(sourceNode);
visited.add(sourceNode.val);
while(!q.isEmpty()) {
TreeNode node = q.remove();
if(node.left == null && node.right == null)
return node.val;
List neighbors = graph.get(node.val);
// add neighbors to the queue
for(TreeNode neighbor : neighbors) {
if(!visited.contains(neighbor.val)) {
q.add(neighbor);
visited.add(neighbor.val);
}
}
}
return -1;
}
private void dfs(TreeNode node, Map> graph, TreeNode parent, int k) {
if(node == null)
return;
if(node.val == k)
sourceNode = node;
graph.putIfAbsent(node.val, new ArrayList());
List neighbors = graph.get(node.val);
if(node.left != null)
neighbors.add(node.left);
if(node.right != null)
neighbors.add(node.right);
if(parent != null) {
neighbors.add(parent);
}
dfs(node.left, graph, node, k);
dfs(node.right, graph, node, k);
}
}
```
### Complexity Analysis :
```
Time Complexity: O(N) where N is the number of nodes in the given input tree.
Space Complexity: O(N), the size of the graph.
```
# References :
1. https://leetcode.com/problems/closest-leaf-in-a-binary-tree/solution
## Similar Question :
All nodes distance k in a binary tree https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree