Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
class Solution:
# @param root, a tree node
# @return an integer
def minDepth(self, root):
if root == None:
return 0
if root.left == None and root.right == None:
return 1
leftDepth=self.minDepth(root.left)
rightDepth = self.minDepth(root.right)
if leftDepth == 0:
return rightDepth + 1
elif rightDepth == 0:
return leftDepth + 1
else:
return min(leftDepth,rightDepth) + 1