102. Binary Tree Level Order Traversal
题目:
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,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
链接: http://leetcode.com/problems/binary-tree-level-order-traversal/
题解:
层序遍历二叉树。使用BFS, 用一个Queue来辅助存储当前层的节点。
Time Complexity - O(n), Space Complexity - O(n) (最多一层节点数)
public class Solution {
public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(root == null)
return result;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
ArrayList<Integer> list = new ArrayList<Integer>();
queue.add(root);
int curLevel = 1, nextLevel = 0; while(!queue.isEmpty()){
TreeNode node = queue.poll();
curLevel --;
list.add(node.val); if(node.left != null){
queue.offer(node.left);
nextLevel ++;
}
if(node.right != null){
queue.offer(node.right);
nextLevel ++;
}
if(curLevel == 0){
curLevel = nextLevel;
nextLevel = 0;
result.add(new ArrayList<Integer>(list));
list.clear();
}
} return result;
}
}
Update:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null)
return res;
Queue<TreeNode> queue = new LinkedList<>(); //BFS
ArrayList<Integer> list = new ArrayList<>();
queue.add(root); //Initialize
int curLevel = 1, nextLevel = 0; while(!queue.isEmpty()) {
TreeNode node = queue.poll();
list.add(node.val);
curLevel --;
if(node.left != null) {
queue.offer(node.left);
nextLevel++;
}
if(node.right != null) {
queue.offer(node.right);
nextLevel++;
}
if(curLevel == 0) {
curLevel = nextLevel;
nextLevel = 0;
res.add(new ArrayList<Integer>(list));
list.clear();
}
} return res;
}
}
二刷:
使用一个queue来帮助BFS层序遍历,有两个记录当前层节点和下一层节点数的变量curLevel以及nextLevel。
Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int curLevel = 1, nextLevel = 0;
List<Integer> list = new ArrayList<>();
while (!q.isEmpty()) {
TreeNode node = q.poll();
curLevel--;
list.add(node.val);
if (node.left != null) {
q.offer(node.left);
nextLevel++;
}
if (node.right != null) {
q.offer(node.right);
nextLevel++;
}
if (curLevel == 0) {
curLevel = nextLevel;
nextLevel = 0;
res.add(new ArrayList<Integer>(list));
list.clear();
}
}
return res;
}
}
三刷:
不要忘记把结果保存到结果集里,并且也要clear当前层。
Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
List<Integer> level = new ArrayList<>();
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int curLevel = 1, nextLevel = 0; while (!q.isEmpty()) {
TreeNode node = q.poll();
curLevel--;
level.add(node.val);
if (node.left != null) {
q.offer(node.left);
nextLevel++;
}
if (node.right != null) {
q.offer(node.right);
nextLevel++;
}
if (curLevel == 0) {
curLevel = nextLevel;
nextLevel = 0;
res.add(new ArrayList<Integer>(level));
level.clear();
}
}
return res;
}
}
Update: 换种写法
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int curLevel = 1;
List<Integer> list = new ArrayList<>(); while (!q.isEmpty()) {
TreeNode node = q.poll();
curLevel--;
list.add(node.val);
if (node.left != null) q.offer(node.left);
if (node.right != null) q.offer(node.right);
if (curLevel == 0) {
curLevel = q.size();
res.add(new ArrayList<>(list));
list.clear();
}
} return res;
}
}
Reference:
https://leetcode.com/discuss/21778/java-solution-using-dfs
https://leetcode.com/discuss/22533/java-solution-with-a-queue-used
https://leetcode.com/discuss/58454/java-queue-solution-beats-100%25
102. Binary Tree Level Order Traversal的更多相关文章
- [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, ...
- 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 (2 solutions)
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- [刷题] 102 Binary Tree Level Order Traversal
要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...
- 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]题解(python):102 Binary Tree Level Order Traversal
题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal/ Given a binary tree, return th ...
- 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, ...
- 【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, ...
- 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 ...
随机推荐
- Powerful Sleep(神奇的睡眠-睡眠生物钟的秘密:如何睡得更少却睡得更好)阅读笔记
睡眠机制 我们活着的时候,大脑会产生脑电波.脑电图仪器通过贴在人头上的一些电极读出脑电波的活动,然后把活动用图表显示出来. 睡眠过程可以分为5个过程,划分依据与大脑发出的脑电波类型. 当人清醒时,大脑 ...
- dom4j api 详解--XPath 节点详解
dom4j api 详解 http://871421448.iteye.com/blog/1546955 XPath 节点 http://www.w3school.com.cn/xpath/xpath ...
- iOS编程——经过UUID和KeyChain来代替Mac地址实现iOS设备的唯一标示(OC版)
iOS编程——通过UUID和KeyChain来代替Mac地址实现iOS设备的唯一标示(OC版) 很多的应用都需要用到手机的唯一标示,而且要求这个唯一标示不能因为应用app的卸载或者改变而变化. 在iO ...
- mysql实例---sql语句中使用@变量
本文介绍下,在mysql语句中使用@变量的一个例子,学习下这个特殊变量的用法,有需要的朋友参考下吧. 要求: 计算用户距上次访问的天数,根据imei号区分不同的用户,如果时间段内只有一次访问则为0. ...
- Eclipse查看源码乱码问题
在同一个工作区下,依赖的Jar包如果既有GBK编码,又有UTF-8编码,就会出现的问题. 常规解决方法 1.切换工作区编码.麻烦,效率低. 2.设置Jar包编码,只有Eclipse4.2支持,4.0以 ...
- centos架设FTP服务器
1.安装vsftp在这里,我们架设的是虚拟用户,所谓虚拟用户就是没有使用真实的帐户,只是通过某种手段达到映射帐户和设置权限的目的.yum -y install vsftpd在CentOS中,这样就可以 ...
- Oracle SGA参数调整
一. SGA的组成: 自动 SGA 管理后,Oracle 可以自动为我们调整以下内存池的大小: shared pool buffer cache large pool java pool stream ...
- 【Python笔记】异常处理
1 什么是异常 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行.一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误. 当Pytho ...
- Eclipse开发Android报错Jar mismatch! Fix your dependencies
常常打开工程,发现项目并没有错,但是会有一个红X,然后就生成不了. 发现两个版本不同的android-support-v4.jar在使用 打开window-show views-problems Ja ...
- 经典好文:android和iOS平台的崩溃捕获和收集
通过崩溃捕获和收集,可以收集到已发布应用(游戏)的异常,以便开发人员发现和修改bug,对于提高软件质量有着极大的帮助.本文介绍了iOS和android平台下崩溃捕获和收集的原理及步骤,不过如果是个人开 ...