107. Binary Tree Level Order Traversal II
题目:
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
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-ii/
题解:
这几道题都差不多,有关level order的。也可能有很好的方法,但是都没仔细想。基本思路和其他一样,就是BFS
Time Complexity - O(n), Space Complexity - O(1)。
/**
* 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>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null)
return res;
ArrayList<Integer> list = new ArrayList<>();
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int curLevel = 1, nextLevel = 0; 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(0, new ArrayList<Integer>(list));
list.clear();
}
} return res;
}
}
二刷 :
方法跟一刷一样,就是跟普通level order traversal一样,但是保存结果的时候点到以下顺序。
Java:
Time Complexity - O(n), Space Complexity - O(1)。
/**
* 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>> levelOrderBottom(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> level = new ArrayList<>();
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(0, new ArrayList<Integer>(level));
level.clear();
}
}
return res;
}
}
题外话: 2/14/2016
时间过得真的很快,不抓紧的话根本刷不完。前面一段时间效率极低,每天一两题,也不愿意花时间刷题,反而停留在舒适区,看看书,看看设计。后来返回来刷题发现根本不熟。
最后祝大家情人节快乐。
三刷:
再看了一下discuss,好像用dfs始终比bfs快.
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>> levelOrderBottom(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();
level.add(node.val);
curLevel--;
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(0, new ArrayList<Integer>(level));
level.clear();
}
}
return res;
}
}
Reference:
https://leetcode.com/discuss/5353/there-better-regular-level-order-traversal-reverse-result
https://leetcode.com/discuss/91100/share-java-2ms-recursive-solution
https://leetcode.com/discuss/22538/my-dfs-and-bfs-java-solution
https://leetcode.com/discuss/81189/java-1ms-beats-98%25-using-preorder
107. Binary Tree Level Order Traversal II的更多相关文章
- 102/107. Binary Tree Level Order Traversal/II
原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...
- 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)
Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- (二叉树 BFS) 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 left ...
- Java for 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 left ...
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...
- [LeetCode]题解(python):107 Binary Tree Level Order Traversal II
题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return ...
- leetcode 107 Binary Tree Level Order Traversal II ----- java
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- 停车场管理软件附带源代码 J2EE服务端+android客户端
该源码是停车场管理软件附带源代码 J2EE服务端+android客户端,也是一套停车场管理车辆进出的管理软,喜欢的朋友可以看看吧. 应用的后台管理主要功能介绍:1 机构管理 ,机构有从属管理< ...
- windows phone URI映射
UriMapping用于在一个较短的URI和你项目中的xaml页的完整路径定义一个映射(别名).通过使用别名URI,开发者可以在不改变导航代码的情况下来改变一个项目的内部结构.该机制还提供了一个简单的 ...
- C#反编译工具 ILSPY-x64可动态调试-君临汉化版
程序基于著名的ILSpy version 2.1.0.1603 汉化,并增加x64下debugging功能;初衷是网上只有一版是原作者留下的x86版本,实在不想在虚拟机里调试,只有自己动手弄一份x64 ...
- iOS Mac系统下Ruby环境安装
由EasyIOS引出的一系列问题:转载的上一篇CocoaPods安装和使用教程中说明了,为什么要使用cocoapods ,但是要安装cocoapods需要Ruby环境,安装Ruby环境首先需要安装Xc ...
- soinn
Growing Cell Structures A Self-Organizing Network for Unsupervised and Supervised Learning Here, and ...
- centos 安装ecshop出现date错误
centos 安装ecshop 错误提示 Warning: date(): It is not safe to rely on the system's timezone settings. You ...
- python--gevent协程及协程概念
何为协程 协程,又称微线程.英文名Coroutine. 协程最大的优势就是协程极高的执行效率.因为子程序切换不是线程切换,而是由程序自身控制,因此,没有线程切换的开销,和多线程比,线程数量越多,协程的 ...
- clients(PV操作共享内核内存进行输入输出分屏) - server(进程间通信)模型实现
1.拓扑结构 2.PV操作共享内核内存进行输入输出分屏 (1) int semop(int semid,struct sembuf *sops,size_t nsops): 功能描述 操作一个或一组信 ...
- AJAX 基础知识
1.AJAX 是一种用于创建快速动态网页的技术.而是一种用于创建更好更快以及交互性更强的 Web 应用程序的技术.通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不 ...
- 百度地图 获取矩形point
http://developer.baidu.com/map/jsdemo.htm#f0_7 鼠标绘制点线面 <!DOCTYPE html><html><head> ...