Return the minimum depth from root to the nearest leaf.
Return 0 for a null node. If one child is null, the minimum depth must go through the other child. Otherwise, take 1 + min(leftDepth, rightDepth).
1 + min(leftDepth, rightDepth)
public int minDepth(TreeNode root) { if (root == null) { return 0; } if (root.left == null) { return 1 + minDepth(root.right); } if (root.right == null) { return 1 + minDepth(root.left); } return 1 + Math.min(minDepth(root.left), minDepth(root.right)); }