#从上往下打印二叉树--队列思想-先进先出,先将根节点放入队列,弹出,之后在遍历根节点的左右节点都放入队列,依次弹出
def OrderPrinTree(root):
if root is None:
return None
queue = []
result = []
queue.append(root)
while queue:
root_v = queue.pop(0)
result.append(root_v.val)
if root_v.left != None:
queue.append(root_v.left)
if root_v.right != None:
queue.append(root_v.right)
return result
print(OrderPrinTree(root1))
遍历二叉树--python
最新推荐文章于 2024-10-21 08:57:50 发布