题目
分析
bfs 遍历每层时 把这一层的节点加入数组,把数组加入结果中
python代码
class Solution:
# 返回二维列表[[1,2],[4,5]]
def Print(self, pRoot):
# write code here
if not pRoot: return []
res = []
arr = [pRoot]
while(len(arr)!=0):
temp_arr = []
temp_res = []
for n in arr:
if n:
temp_res.append(n.val)
if n.left:
temp_arr.append(n.left)
if n.right:
temp_arr.append(n.right)
res.append(temp_res)
arr = temp_arr
return res