Java:
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
int l = minDepth(root.left);
int r = minDepth(root.right);
return (l == 0 || r == 0) ? l + r + 1:Math.min(l, r) + 1;
}
}
Python:
class Solution:
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0
l, r = self.minDepth(root.left), self.minDepth(root.right)
return l+r+1 if l==0 or r==0 else min(l, r) + 1