# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # 返回从上到下每个节点值列表,例:[1,2,3] def PrintFromTopToBottom(self, root): # write code here if root == None: return [] # 构造一个辅助list存访节点 support = [root] # 返回的数字 ret = [] while support: tempnode = support[0] ret.append(tempnode.val) if tempnode.left != None: support.append(tempnode.left) if tempnode.right != None: support.append(tempnode.right) del support[0] return ret
原文地址:https://www.cnblogs.com/ivyharding/p/11351038.html
时间: 2024-10-03 20:57:12