Find nearest leaf from a node in binary tree.

+2 votes
asked in Interview Question by admin
You have a binary tree, find the minimum length from the input node to leaf.
Note: It does not have any parent pointer.A node of tree has only left, right and node value information.
Add question to:

2 Answers

+1 vote
answered by allenite
We can make adjacency list to store the graph, and then apply BFS from the input node.

As BFS increases level by level, for each node in particular level we check if the size of a vector is one then print that level of that node(i.e. the ans).
commented by admin
You are correct. I have changed question, so how to solve this without repopulating given tree in any other structure.
commented by allenite
We can apply bfs/dfs from root and then we store parent of the child and then make adjency list using it and then apply the above concept
0 votes
answered by sheik

Explanation: the above program uses recursion to find the minimum length from any given node to a leaf in the Binary tree.
Find length of left subTree
Find length of right subTree
add one to a smallest of the two to find the actual length from node to the leaf

Note: sample binary tree is not formed in this example assuming binary tree is present and 3 is one value of it

Language: Java

class Test{

int minLength(int node){

return -1;

int leftLength=minLength(node.Left);
int rightLength=minLength(node.right);

return min(leftLength, rightLength)+1;

min(int a, int b){
return a;
return b;
public static void main(String[] args){
int node=3;
system.out.Println( minLength(node) );