# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
# if not root.left:
# return self.minDepth(root.right) + 1
# if not root.right:
# return self.minDepth(root.left) + 1
# return min(self.minDepth(root.left), self.minDepth(root.right)) + 1
left = Solution.minDepth(self,root.left)
right = Solution.minDepth(self,root.right)
if not left:
return right + 1
if not right:
return left + 1
return min(left, right) + 1