题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
解题思路
按广度优先搜索遍历每一层
python 代码
# -*- 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 []
nodeStack = [pRoot]
result = []
while nodeStack:
res = []
nextStack = []
for i in nodeStack:
res.append(i.val)
if i.left:
nextStack.append(i.left)
if i.right:
nextStack.append(i.right)
nodeStack = nextStack
result.append(res)
return result
s = Solution()
res = s.Print([10,6,14,4,8,12,16])
print('.....',res)