Minimum Depth of a Binary Tree | Practice | GeeksforGeeks

Given a binary tree, find its minimum depth.
Example 1:
Input:
            1
          /   \
         3     2
        /
       4           

Output: 2

Explanation:
Minimum depth is between nodes 1 and 2 since
minimum depth is defined as  the number of 
nodes along the shortest path from the root 
node down to the nearest leaf node.

Example 2:
Input:
             10
          /     \
        20       30
          \        \   
          40        60 
                   /
                  2 

Output: 3

Explanation:
Minimum depth is between nodes 10,20 and 40.

Your Task:  
You don't need to read input or print anything. Complete the function minDepth() which takes the root node as an input parameter and returns the minimum depth.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(height of the tree)
 
Constraints:
1 ≤ N ≤ 10^5
0
devashishbakare1 month ago
JAVA SOLUTION WITH EXPLANATION
 
while trying to do a normal Min(left call + right call + 1) then we have very less count because a root have null to left or right then we consider only null element due than required answer change   
While solving this problem we have some cases as discuss below   int minDepth(Node root){    if(root == null) return 0;        //if we reach out to null node then we have to return null     //no need to count coz it leaf node
    if(root.left==null && root.right== null)        return 1;        //left is not null and right is null Math.min will not work it update less count hence final count is reduced
    else if(root.left!=null && root.right== null)        return minDepth(root.left)+1;      //right is not null and left is null Math.min will not work it update less count hence final count is reduced  
    else if(root.left==null && root.right!= null)        return minDepth(root.right)+1;        //if both not null means there is proper subtree from left side and right side and we have required minimum so used basic formula and consider root itself hence +1        else return Math.min(minDepth(root.left), minDepth(root.right)) + 1;    }
 
 
0
detroix071 month ago
class Solution { public:   /*You are required to complete this method*/
   void dfs(Node* root , int level, int &d) {     // base case      if(root==NULL) return;     // when encounter with leaf node , it can be one of my answer      if(!root->left && !root->right) {       if(level < d) {        d=level;       }     }          dfs(root->left,level+1,d);     dfs(root->right,level+1,d);        }      int minDepth(Node *root) {          int d = INT_MAX;     dfs(root,0,d);     return d+1;        }};
0
manudatta4u1 month ago
Python3 solution:
Time taken: 0.6/2.2
 
class Solution:
    def minDepth(self, root):
        if root is None:
            return 0
            
        left = self.minDepth(root.left)
        right = self.minDepth(root.right)
        
        if(root.left and root.right):
            return 1 + min(left, right)
        elif(root.left):
            return 1+ left
        else:
            return 1 + right
0
redeagle172 months ago
class Solution{ public:   /*You are required to complete this method*/   int minDepth(Node *root) {       // Your code here       if(root==NULL){           return 0;       }       int l=minDepth(root->left);       int r=minDepth(root->right);       if(l==0 || r==0){           return 1+max(l,r);       }       return 1+min(l,r);   }};
0
imaniket2 months ago
int minDepth(Node *root) {
       if(root->left==NULL && root->right==NULL) return 1;
       int left = root->left ? 1+minDepth(root->left): INT_MAX;
       int right = root->right ? 1+minDepth(root->right): INT_MAX;
       return min(left, right);
    }
+1
amanpandey30072 months ago
int solve(Node*root)   {       if(!root)       return 0;              int l=solve(root->left);       int r=solve(root->right);              if(l==0 and r==0)       return 1;              if(l==0)       return r+1;              if(r==0)       return l+1;              return min(l,r)+1;                 }   int minDepth(Node *root)    {              return solve(root);   } 
0
dronzerdracel2 months ago
My simple solution using helper function
class Solution{
  public:
    /*You are required to complete this method*/
    void min(Node* root,int level,int &minm){
        if(!root)
         return;
        if(!root->left&&!root->right){
            if(level<minm)
             minm=level;
        } 
        min(root->left,level+1,minm);
        min(root->right,level+1,minm);
    }
    int minDepth(Node *root) {
        // Your code here
        if(!root)
         return 0;
        int minm=INT_MAX;
        min(root,0,minm);
        return 1+minm;
    }
};
0
tanujrajput00002 months ago
 int minDepth(Node *root) {
        // Your code here
        if(!root) return 0;
        int left = minDepth(root->left);
        int right = minDepth(root->right);
        if(root->left and root->right) return 1 + min(left,right);
        else if(root->left) return 1 + left;
        else return 1 + right;
    }
+1
ashiqhussainkumar1433 months ago
    int minDepth(Node *root) {       // Your code here       if(!root) return 0;       int left=minDepth(root->left);       int right=minDepth(root->right);       if(root->left and root->right) return 1+min(left , right);       else if(root->left) return 1+left;       else return 1+right;   }
0
namanshah22753 months ago
cpp solution time O(N) 0.2 sec space O(h)
 
public:   /*You are required to complete this method*/   int minDepth(Node *root) {              if(root == NULL)       return 0;              int l = minDepth(root->left);       int r = minDepth(root->right);             if(root->left == NULL || root->right == NULL)      return 1+max(l,r);                  return 1+min(l,r);   }
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.