Guys is my code so wrong or it can be modified and work ? it says wrong output on test case {1,2}


  • 0
    N
     public class Solution
    
    {
    
    public int MinDepth(TreeNode root) 
    
    {
    
        if(root == null)
    
        return 0;
    
        else
    
      return CalculateMinDepth(root,1);
    
    }
    
    public int CalculateMinDepth(TreeNode root , int initialDepth){
     
     if(root == null)
     {
         return initialDepth;
     }
     
     int left = initialDepth; 
     int right = initialDepth;
     
     if(root.left==null && root.right==null)
     {
         return 1;
     }
     
     if(root.left != null)
    left = CalculateMinDepth(root.left, left+1);
     
     
     if(root.right != null)
    right=  CalculateMinDepth(root.right , right+1);
    
    
     if( left > right)
     return right;
     else
     return left;
     }
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.