题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
思路:
层次遍历,利用列表进行辅助
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回二维列表[[1,2],[4,5]]
def Print(self, pRoot):
# write code here
if not pRoot:
return []
p = []
p.append(pRoot)
result = []
while p:
temp = []
x = []
for i in p:
temp.append(i.val)
if i.left:
x.append(i.left)
if i.right:
x.append(i.right)
p = x
result.append(temp)
return result