Binary tree binary tree from the top down _ Print
Description Title
print out from the top of each node of the binary tree, with the layer node from left to right printing.
Problem Analysis
# -*- 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
queue = []
result = []
if not root:
return result
queue.append(root)
while queue:
newnode = queue.pop(0)
result.append(newnode.val)
if newnode.left:
queue.append(newnode.left)
if newnode.right:
queue.append(newnode.right)
return result