Leetcode 102 二叉树的层次遍历 Python
2024-08-23 02:11:36
二叉树的层次遍历
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
例如: 给定二叉树: [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
最新文章
- (并查集)~APTX4869(fzu 2233)
- grunt安装_
- Cocos2d-JS cc.DrawNode用法
- Struts2之异常处理
- JDK Tools - jps: JVM 进程状态工具
- Android三种实现自定义ProgressBar的方式介绍
- 封装函数--->;切换,添加,删除class
- Singleton(单例)模式
- makefile 中=与:=的差别
- C# RichTextBox 滚动条 滚动到最新行
- python中定时任务
- vi/vim 命令速查手册
- remote desktop software
- 使用Snap.svg类库实现的抖动式的幻灯播放效果
- JVM(三)调优工具
- 常用PHP文件操作函数
- 【BZOJ4101】[Usaco2015 Open]Trapped in the Haybales Silver 二分
- 20155235 2016-2017-2 《Java程序设计》第十周学习总结
- http://my.oschina.net/China2012/blog/178655
- VS下QT的自定义槽函数修改方法
热门文章
- 滑动窗口的中位数 · Sliding Window Median
- WebService超时
- halcon的tuple算子功能总结
- Windows本地Linux虚拟机ping不通的解决办法
- Linux 启动和关闭自定义命令
- 解决 Laravel try catch 不工作的问题
- [Training Video - 5] [Groovy Script Test Step - Collections, Exceptions] Array and ArrayList
- 利用FFmpeg转压视频的说明
- tips 移出 消失和 移入 显示
- HBase & thrift & C++编程