牛客网链接
# -*- 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
#共使用4个List:使用两个List储存当前层节点、下一层节点;使用两个List储存每层val、所有层val
if pRoot == None : return []
res=[]
currentNodes=[pRoot]
while currentNodes:
nextNodes=[]
currentVal=[]
for node in currentNodes:
currentVal.append(node.val)
if node.left:
nextNodes.append(node.left)
if node.right:
nextNodes.append(node.right)
res.append(currentVal)
currentNodes=nextNodes
return res