这段代码如何找到二叉树的最小深度?
How does this code work to find the Minimum Depth of Binary Tree?
我从
https://leetcode.com/discuss/37282/simple-python-recursive-solution-bfs-o-n-80ms
这是
的答案
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from
the root
node down to the nearest leaf node.
class Solution:
# @param {TreeNode} root
# @return {integer}
def minDepth(self, root):
if not root:
return 0
nodes = [(root, 1)]
while nodes:
node, curDepth = nodes.pop(0)
if node.left is None and node.right is None:
return curDepth
if node.left:
nodes.append((node.left, curDepth + 1))
if node.right:
nodes.append((node.right, curDepth + 1))
所以我的困惑是,如果节点 1 有节点 2 和节点 3 作为其 .left 和 .right children,那么堆栈将是 [(node 2, someDepth), (node 3一些深度)]。然后由于堆栈只会弹出列表的最后一个元素,因此 (node 3 someDepth) 将被解包,而 node 2 将被完全忽略。那么如果节点2没有child,而节点3有,那么后续迭代使用节点3是不是错了?
你漏掉的是
nodes.pop(0)
弹出第 0 个元素。
所以你错了:
Then as the stack would only pop out the last element of the list, then...
想象一棵二叉树:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ /
8 9 10 11 12
此处状态 space 将更改为(为简单起见,节点以其内容编号命名):
# Before 1st iteration.
nodes = [(1, 1)]
# 1st iteration.
node, curDepth = 1, 1
nodes = [(2, 2), (3, 2)]
# 2nd iteration.
node, curDepth = 2, 2
nodes = [(3, 2), (4, 3), (5, 3)]
# 3rd iteration.
node, curDepth = 3, 2
nodes = [(4, 3), (5, 3), (6, 3), (7, 3)]
# 4th iteration.
node, curDepth = 4, 3
nodes = [(5, 3), (6, 3), (7, 3), (8, 4), (9, 4)]
# 5th iteration.
node, curDepth = 5, 3
# Here if node.left is None and node.right is None becomes True and curDepth i.e. 3 is returned.
可以看出,节点是按(树的)广度处理的,因此它是 BFS。
递归解更容易理解。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param {TreeNode} root
# @return {integer}
def minDepth(self, root):
if root is None:
return 0
if root.left is None:
return 1 + self.minDepth(root.right)
if root.right is None:
return 1 + self.minDepth(root.left)
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
我从 https://leetcode.com/discuss/37282/simple-python-recursive-solution-bfs-o-n-80ms
这是
的答案Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root
node down to the nearest leaf node.
class Solution:
# @param {TreeNode} root
# @return {integer}
def minDepth(self, root):
if not root:
return 0
nodes = [(root, 1)]
while nodes:
node, curDepth = nodes.pop(0)
if node.left is None and node.right is None:
return curDepth
if node.left:
nodes.append((node.left, curDepth + 1))
if node.right:
nodes.append((node.right, curDepth + 1))
所以我的困惑是,如果节点 1 有节点 2 和节点 3 作为其 .left 和 .right children,那么堆栈将是 [(node 2, someDepth), (node 3一些深度)]。然后由于堆栈只会弹出列表的最后一个元素,因此 (node 3 someDepth) 将被解包,而 node 2 将被完全忽略。那么如果节点2没有child,而节点3有,那么后续迭代使用节点3是不是错了?
你漏掉的是
nodes.pop(0)
弹出第 0 个元素。
所以你错了:
Then as the stack would only pop out the last element of the list, then...
想象一棵二叉树:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ /
8 9 10 11 12
此处状态 space 将更改为(为简单起见,节点以其内容编号命名):
# Before 1st iteration.
nodes = [(1, 1)]
# 1st iteration.
node, curDepth = 1, 1
nodes = [(2, 2), (3, 2)]
# 2nd iteration.
node, curDepth = 2, 2
nodes = [(3, 2), (4, 3), (5, 3)]
# 3rd iteration.
node, curDepth = 3, 2
nodes = [(4, 3), (5, 3), (6, 3), (7, 3)]
# 4th iteration.
node, curDepth = 4, 3
nodes = [(5, 3), (6, 3), (7, 3), (8, 4), (9, 4)]
# 5th iteration.
node, curDepth = 5, 3
# Here if node.left is None and node.right is None becomes True and curDepth i.e. 3 is returned.
可以看出,节点是按(树的)广度处理的,因此它是 BFS。
递归解更容易理解。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param {TreeNode} root
# @return {integer}
def minDepth(self, root):
if root is None:
return 0
if root.left is None:
return 1 + self.minDepth(root.right)
if root.right is None:
return 1 + self.minDepth(root.left)
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))