题目:

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的更多相关文章

  1. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

  2. 【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 ...

  3. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  4. (二叉树 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 ...

  5. 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 ...

  6. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

  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 ...

  8. 剑指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 ...

  9. [LeetCode]题解(python):107 Binary Tree Level Order Traversal II

    题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return ...

  10. 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 ...

随机推荐

  1. C++与Lua交互(三)

    通过上一篇的热身,我们对C++调用lua变量有了一个认识,现在让我们再深入一点,去探索一下如何调用lua的函数.表. Lua与宿主通讯的关键--栈 lua是个动态脚本语言,它的数据类型如何映射到C++ ...

  2. L001-老男孩教育-Python13期VIP视频-19节-pbb

    L001-老男孩教育-Python13期VIP视频-19节-pbb Windows上安装 Python3开发环境 下载:www.python.org >选择Downloads>All re ...

  3. PHP的基础计算器

    设计一个计算的功能,该功能能够完成运算并且能够对不合理的数据进行验证并且给出错误提示. 规则: 第一个数,第二个数不能够为空 如果操作符是/,第二个数数不能够为0. <?php header(' ...

  4. 九度OJ1172--哈夫曼树

    哈夫曼树,第一行输入一个数n,表示叶结点的个数.需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和. 输入: 输入有多组数据.每 ...

  5. php截取字符串的实例代码(支持utf-8)

    分享下php中截取字符串的例子,支持utf-8格式. 1,截取字符串 <?php $string="2006年4月我又长大了一岁!"; echo substr($string ...

  6. Kakfa揭秘 Day5 SocketServer下的NIO

    Kakfa揭秘 Day5 SocketServer下的NIO 整个Kafka底层都是基于NIO来进行开发的,这种消息机制可以达到弱耦合的效果,同时在磁盘有很多数据时,会非常的高效,在gc方面有非常大的 ...

  7. Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)

    Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)

  8. JS 原型链图形详解

    JS原型链 这篇文章是「深入ECMA-262-3」系列的一个概览和摘要.每个部分都包含了对应章节的链接,所以你可以阅读它们以便对其有更深的理解. 对象 ECMAScript做为一个高度抽象的面向对象语 ...

  9. kruskal算法-Pascal

    马上就快要考试了,然而突然发现自己图论已经废了,于是再都打一遍练练手...... const maxn=; maxe=maxn*maxn; type edge=record //edge记录每一条边, ...

  10. 操作邮箱的类和文件的Md5【粘】

     MailMessage mailMsg = new MailMessage();//两个类,别混了,要引入System.Net这个Assembly             mailMsg.From ...