这题用的是广度抖索的方法。代码如下:
# 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 = []
二叉树最小深度广度搜索算法
本文介绍了一种使用广度优先搜索算法求解二叉树最小深度的方法,并提供了一个具体的Python实现示例。该算法适用于寻找从根节点到最近叶子节点的最短路径。
1358

被折叠的 条评论
为什么被折叠?



