111 minimum depth of binary tree

111. Minimum Depth of Binary Tree

Easy

class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
return 1 + min(map(self.minDepth, (root.left, root.right)))


1
\
2


2是一个孩子节点

leaf node: itself is not null but it has both children null

# Definition for a binary tree node.
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
depth_under_root = map(self.minDepth, (root.left, root.right))
return 1 + (min(depth_under_root) or max(depth_under_root))


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))