My Java code using DFS


  • 1
    V
    int min = Integer.MAX_VALUE;
    
    public int minDepth(TreeNode root) {
        if (root ==null){
            return 0;
        }
        dfs(root,1);
        return min;
    }
    
    public void dfs(TreeNode node, int height){
        if (node.left!=null ) {
            dfs(node.left, height + 1);
        }
        if(node.right !=null){
            dfs(node.right,height+1);
        }
        if (node.left == null && node.right==null){
            min = Math.min(min,height);
        }
    }

Log in to reply
 

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