[LeetCode] 102. Binary Tree Level Order Traversal_Medium tag: BFS
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
] 这个题目非常明显的适用BFS, 从root, 依次将每一层append进入ans里, 只是要注意的就是如果判断哪个是在哪一层呢, 这里用到的方法是利用size of queue, 每次得到的size就是该层的数目,
每层用一个temp list去存每一层的元素, 结束之后append进入ans中. 1. Constraints
1) tree 可以为empty, 所以edge case 为root == None 2. Ideas BFS T: O(n) S: O(n) n is number of nodes in the tree 3. code
import collections
class Solution:
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root: return []
ans, queue = [], collections.deque([root])
while queue:
size, level = len(queue), []
for _ in range(size):
node = queue.popleft()
level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
ans.append(level)
return ans
4. Test cases
1) None => []
2) [1] => [[1]]
3)
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
[LeetCode] 102. Binary Tree Level Order Traversal_Medium tag: BFS的更多相关文章
- leetcode 102 Binary Tree Level Order Traversal(DFS||BFS)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS
二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
- [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- leetcode 102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- leetcode 102 Binary Tree Level Order Traversal ----- java
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Java [Leetcode 102]Binary Tree Level Order Traversal
题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Java for LeetCode 102 Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
随机推荐
- springbatch---->springbatch的使用(一)
这里我们通过一个简单的实例来对springbatch做一个入门的体会.刚刚好,看见你幸福的样子,于是幸福着你的幸福. springbatch的入门案例 简单项目的结构如下: 一.引入maven的spr ...
- 深入理解 Neutron -- OpenStack 网络实现(1):GRE 模式
问题导读1.什么是VETH.qvb.qvo?2.qbr的存在的作用是什么?3.router服务的作用是什么? 如果不具有Linux网络基础,比如防火墙如何过滤ip.端口或则对openstack ovs ...
- Javascript 细节优化技巧(转)
break 语句和 continue 语句 break语句和continue语句都具有跳转作用,可以让代码不按既有的顺序执行. break语句用于跳出代码块或循环. var i = 0; while( ...
- Java初学者都必须理解的六大问题 (整理自网络)
问题一:我声明了什么! 1. String s = "Helloworld!"; 许多人都做过这样的事情,但是,我们到底声明了什么?回答通常是:一个String,内容是“Hello ...
- openvpn 负载均衡方案
这些方案的前提是,vpnserver的key都是一样的.方案1在openvpn客户端设两个配置文件,我们自己手动去连接去选择方案2在openvpn 的配置文件里面加个随机参数remote 8.8.8. ...
- Linux 下 SVN 命令操作详解
1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/domain ...
- 2015.7.10js-07(简单时间)
今天学习了一个小程序,将本地时间显示在页面上,用了图片的形式. 1.执行原理是,先用6张全0的图片,然后通过循环img各自根据时间来更换相对应的时间图片. 2.使用Date()函数获取本地时间,然后转 ...
- linux shell脚本中 mode=${1:-sart} filename=${fileuser:-"filename"}
$1代表第二个参数m=${1:-start}表示如果$1存在且不为空,m就是$1如果$1不存在或为空,那么m就是start filename=${fileuser:-"filename&qu ...
- C# 压缩文件 ICSharpCode.SharpZipLib.dll
效果: 代码只能压缩文件夹里面的文件,不能压缩文件夹. 压缩前: 压缩后: 代码: 需要引用ICSharpCode.SharpZipLib.dll public ActionResult Index( ...
- mysql rowid实现
) a, b b表是数据表