【题目】从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
【思路】具体思路参考59题,本题不需要对偶数行进行翻转。
【题目】
class Solution:
# 返回二维列表[[1,2],[4,5]]
def Print(self, pRoot):
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