111. Minimum Depth of Binary Tree
题目:
https://leetcode.com/problems/minimum-depth-of-binary-tree/
难度:
Easy
注意leaf node反正就是没有left和right的
比如下图
1
\
2
2是一个孩子节点
class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root == None:
return 0
elif root.left == None and root.right == None:
return 1
else :
if root.left == None:
return 1 + self.minDepth(root.right)
elif root.right == None:
return 1 + self.minDepth(root.left)
else:
return min(1+ self.minDepth(root.left), 1+ self.minDepth(root.right))
网友评论