这题用的是广度抖索的方法。代码如下:
# 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 root == None:
return 0
a = [root]
num = 1
b = []
while True:
for i in a:
if i.left or i.right:
if i.left:
b.append(i.left)
if i.right:
b.append(i.right)
else:
return num
else:
num = num + 1
a = b[::]
b = []