题解 | #二叉树的最小深度#
二叉树的最小深度
http://www.nowcoder.com/practice/6a7f40d7696d46f79c74c61179993be6
思路:使用层次遍历的思想 当 节点左右孩子都是空返回当前层
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def run(self , root: TreeNode) -> int:
if root is None:return 0
stack=[root]
ceng=0
while stack:
for i in range(len(stack)):
node=stack.pop(0)
if node.left:stack.append(node.left)
if node.right:stack.append(node.right)
if node.left is None and node.right is None:return ceng+1
ceng+=1
return ceng+1