[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] ]
转载于:https://www.cnblogs.com/Johnsonxiong/p/9261520.html
[LeetCode] 102. Binary Tree Level Order Traversal_Medium tag: BFS相关推荐
- LeetCode 102. Binary Tree Level Order Traversal--递归,迭代-Python,Java解法
题目地址: 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 rig ...
- 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 rig ...
- LeetCode: 107. Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode 107. Binary Tree Level Order Traversal II
LeetCode 107. Binary Tree Level Order Traversal II Solution1:我的答案 比102那道题多了一行代码... /*** Definition f ...
- 102. Binary Tree Level Order Traversal
题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...
- Leet Code OJ 102. Binary Tree Level Order Traversal [Difficulty: Easy]
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
最新文章
- Redis初学:2(Redis的启动)
- USACO Shaping Regions(离散化)
- RemoteApp连接时所需要的证书
- word 产生很多temp 不显示_word表格中文字显示到最下面的时候不自动换页-解决办法...
- Linux 配置Tomcat
- 《深入理解Hadoop(原书第2版)》——3.4第一个Hadoop程序
- js 判断是否为mac电脑 、还是windows操作系统
- 网络嗅探器c语言代码,基于C 网络嗅探器的设计与实现-精品.doc
- 一个函数搞定无限层级分类
- Unity3D陀螺仪的使用
- 从高盛AI报告看国内智能语音翘楚 但没有科大讯飞
- webpack整合bable
- 数据中台的云原生机会 | 甲子光年
- python-完整的for循环语法
- (转)Serializable 序列化和反序列化
- 文字翻译器有哪些?文字翻译器哪个好?
- XSS挑战之旅[全20关]上
- 如何设置和开始使用Synology NAS
- 快播CEO王欣出狱;1号单车宣布停运;网易考拉聚美等海淘商品涉假 丨价值早报
- 服务器系统整机拷贝,服务器主机整机拷贝
热门文章
- WinCE驱动编写小结
- 使用 jQuery 简化 Ajax 开发.
- 河南大学明德计划2020计算机学院,关于选拔2020级物理学“明德计划”实验班学生的通知...
- Linux创建文件自动消失,Linux中没有文件创建时间的概念
- ab的压力测试(转)
- [React Router v4] Conditionally Render a Route with the Switch Component
- iptables一次性封多个ip,使用ipset 工具
- 数据库死锁的解决办法
- Atitit. atiJavaExConverter4js 新的特性
- LinearGradient线性渲染