Simple java solution.


  • 3
    P
    private int min = Integer.MAX_VALUE;
    
    public int minDepth(TreeNode root) {
    	if (root == null) return 0;
    	findMinDepth(root, 1);
    	return min;
    }
    
    private void findMinDepth(TreeNode node, int level) {
    	if (level >= min || node == null) return;
    	if (node.left == null && node.right == null && level < min) {
    		min = level;
    	}
    	findMinDepth(node.left, level+1);
    	findMinDepth(node.right, level+1);
    }

Log in to reply
 

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