题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
说明:本题目包含复杂数据结构TreeNode,点此查看相关信息
# -*- 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 []
ret = []
curr_stack = [pRoot]
while curr_stack:
next_stack = []
curr_val = []
for node in curr_stack:
if node.left:
next_stack.append(node.left)
if node.right:
next_stack.append(node.right)
curr_val.append(node.val)
curr_stack = next_stack
ret.append(curr_val)
return ret