leetcode之Minimum Depth of Binary Tree

来源:互联网 发布:淘宝网购物步骤 编辑:程序博客网 时间:2024/06/03 16:47
这题用的是广度抖索的方法。代码如下:
# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass 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 = []

0 0
原创粉丝点击