Simple java code


  • 0
    M

    public int minDepth(TreeNode root) {

    if(root==null){
        return 0;
    }
    
    int left=minDepth(root.left);
    int right=minDepth(root.right);
    if(root!=null&&root.left!=null&&root.right==null){
        return left+1;
    }
     if(root!=null&&root.left==null&&root.right!=null){
        return right+1;
    }
    return Math.min(left,right)+1;
    }

Log in to reply
 

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