【python-leetcode111-树的宽度遍历】二叉树的最小深度
发布日期:2021-05-09 08:30:44 浏览次数:18 分类:博客文章

本文共 933 字,大约阅读时间需要 3 分钟。

������������������������������������������������

������������������������������������������������������������������������������������

������: ���������������������������������������������

������:

��������������� [3,9,20,null,null,15,7],

3

/ \
9 20
    / \
  15 7
������������������������  2.

 

# Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution:    def minDepth(self, root: TreeNode) -> int:        if  not root:            return 0        queue=[root]        res=[]        depth=0        while queue:            depth=depth+1            l=len(queue)            for i in range(l):                t=queue.pop(0)                if not t.left and not t.right:                    return depth                if t.left:                    queue.append(t.left)                if t.right:                    queue.append(t.right)

 

上一篇:【python-leetcode113-树的深度遍历】路径总和Ⅱ
下一篇:【python-leetcode637-树的宽度遍历】二叉树的层平均值

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年04月29日 18时01分18秒