Share my clean Java code


  • 13
    T
    public int minDepth(TreeNode root){
        if(root == null){
            return 0;
        }
        return helper(root);
    }
    
    public int helper(TreeNode root) {
        if(root == null){
            return Integer.MAX_VALUE;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        
        int depthLeft = helper(root.left);
        int depthRight = helper(root.right);
        
        return Math.min(depthLeft, depthRight) + 1;
    }

Log in to reply
 

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