层序遍历 树
import collections
class Solution:
# 返回从上到下每个节点值列表,例:[1,2,3]
def PrintFromTopToBottom(self, root):
# write code here
queue=collections.deque([root])
visit=[]
result=[]
if root==None:
return result
while queue:
root=queue.popleft()
if root.left:
queue.append(root.left)
if root.right:
queue.append(root.right)
if root not in visit:
result.append(root.val)
visit.append(root)
return result

京公网安备 11010502036488号