从上往下打印二叉树
2019-12-27 本文已影响0人
而立之年的技术控
afd12d0913564b448442e4eff27e62fd.jpg
class Solution:
# 返回从上到下每个节点值列表,例:[1,2,3]
def PrintFromTopToBottom(self, root):
# write code here
if not root:
return []
stack = [root]
ret = []
while stack:
tmp = stack[0]
ret.append(tmp.val)
if tmp.left:
stack.append(tmp.left)
if tmp.right:
stack.append(tmp.right)
del stack[0]
return ret