标签:append not utf-8 bsp offer printf from 打印二叉树 tree
面试32题:
题目:从上到下打印二叉树
题:不分行从上到下打印二叉树
解题代码:
# -*- 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 not root: return [] res=[] res_val=[] res.append(root) while len(res)>0: node=res.pop(0) res_val.append(node.val) if node.left: res.append(node.left) if node.right: res.append(node.right) return res_val
标签:append not utf-8 bsp offer printf from 打印二叉树 tree
原文地址:https://www.cnblogs.com/yanmk/p/9217654.html