二叉树的层次遍历

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

例如: 给定二叉树: [3,9,20,null,null,15,7],

    3
  / \
9 20
  / \
  15   7
返回其层次遍历结果:
[
[3],
[9,20],
[15,7]
]
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if root == None:
return []
layer = [root]
res = []
while len(layer):
this_res = []
next_l = []
for n in layer:
this_res.append(n.val)
if n.left:
next_l.append(n.left)
if n.right:
next_l.append(n.right)
res.append(this_res)
layer = next_l
return res
 

最新文章

  1. (并查集)~APTX4869(fzu 2233)
  2. grunt安装_
  3. Cocos2d-JS cc.DrawNode用法
  4. Struts2之异常处理
  5. JDK Tools - jps: JVM 进程状态工具
  6. Android三种实现自定义ProgressBar的方式介绍
  7. 封装函数--->切换,添加,删除class
  8. Singleton(单例)模式
  9. makefile 中=与:=的差别
  10. C# RichTextBox 滚动条 滚动到最新行
  11. python中定时任务
  12. vi/vim 命令速查手册
  13. remote desktop software
  14. 使用Snap.svg类库实现的抖动式的幻灯播放效果
  15. JVM(三)调优工具
  16. 常用PHP文件操作函数
  17. 【BZOJ4101】[Usaco2015 Open]Trapped in the Haybales Silver 二分
  18. 20155235 2016-2017-2 《Java程序设计》第十周学习总结
  19. http://my.oschina.net/China2012/blog/178655
  20. VS下QT的自定义槽函数修改方法

热门文章

  1. 滑动窗口的中位数 · Sliding Window Median
  2. WebService超时
  3. halcon的tuple算子功能总结
  4. Windows本地Linux虚拟机ping不通的解决办法
  5. Linux 启动和关闭自定义命令
  6. 解决 Laravel try catch 不工作的问题
  7. [Training Video - 5] [Groovy Script Test Step - Collections, Exceptions] Array and ArrayList
  8. 利用FFmpeg转压视频的说明
  9. tips 移出 消失和 移入 显示
  10. HBase & thrift & C++编程