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,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

[
[15,7],
[9,20],
[3]
]

题目标签:Tree
  先来说点有的没的,这是美国的一个长周末,国庆节,大家都出去到处野到处浪了,嗯,我在刷题找工作。玩还是要玩的,一周一天吧,一次出去个3,4天目前心里不踏实,玩不出劲来呢!  
 
  回到题目, 这道题目给了我们一个二叉树,让我们返回一个list<list>,每一个子list 包含一个level的所有点的值,总list 的顺序是从树的底部到顶部。那么我们需要另外一个function 叫 levelDown, 从树的top开始遍历到bottom(preOrder),这个function 除了node, 还有一个带入值,就是level 的值,(0,1,2,。。。) 这样就可以让每一个点确定它在哪个level,对于每一个点,把它的值存进相对应的level的那个子list就可以了。不过要注意的是,因为原来总list里都是空的,一个子list也没有,所以当遇到一个新的level的时候,要加入新的子list,当之后另外同样level的点加入的时候,需要先get到这个子list,然后在子list里加入。所以对于每一个点,需要判断一下,属于它level的list 有没有,没有就加个新的list,有就get到直接加入。 最后完成了总list,把list reverse一下就可以了。
 
 

Java Solution:

Runtime beats 71.19%

完成日期:07/02/2017

关键词:Tree

关键点:把树的level值代入recursive function;对于每一个点,需要判断属于它的子list是否存在

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> levelOrderBottom(TreeNode root)
{
if(root == null)
return res; levelDown(root, 0); Collections.reverse(res); return res;
} public void levelDown(TreeNode node, int level)
{
if(node == null)
return; if(res.size() == level) // meaning this level list is empty, need to add new list
{
List<Integer> temp = new ArrayList<>();
temp.add(node.val);
res.add(level, temp);
}
else
{
res.get(level).add(node.val);
} levelDown(node.left, level+1);
levelDown(node.right, level+1); return; }
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 107. Binary Tree Level Order Traversal II (二叉树阶层顺序遍历之二)的更多相关文章

  1. leetcode 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II

    相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

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

  3. Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS

    题意是倒过来层次遍历二叉树 下面我介绍下BFS的基本框架,所有的BFS都是这样写的 struct Nodetype { int d;//层数即遍历深度 KeyType m;//相应的节点值 } que ...

  4. 107 Binary Tree Level Order Traversal II 二叉树的层次遍历 II

    给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶节点所在层到根节点所在的层,逐层从左向右遍历)例如:给定二叉树 [3,9,20,null,null,15,7],    3   / \  9 ...

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

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

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

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

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

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

随机推荐

  1. Java:print、printf、println的区别

    printf主要是继承了C语言的printf的一些特性,可以进行格式化输出 print就是一般的标准输出,但是不换行 println和print基本没什么差别,就是最后会换行 System.out.p ...

  2. 超简单的js评价小星星

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. MySQL线程池的引入可以提高我们的MySQL的性能

    支持线程池的版本:MySQL 企业版本,MySQL percona的分支 MariDB 的版本.我们知道我们的MySQL 语句是不支持硬解析的,没有无SQL 解析 cache.每个连接对应一个线程,我 ...

  4. unity3D写一个hello world

    unity3D写一个hello world 打开unity并且在assets建立一个新的文件,新的文件命名为hello world.unity.接着创建一个新的C#Sript脚本文件,命名为hello ...

  5. Microsoft Visual Studio调试监视器(MSVSMON.EXE)未能启动

    在启动VS2010项目时,遇到如图所示"Microsoft Visual Studio调试监视器(MSVSMON.EXE)未能启动"的问题. 原因是VS2010安装路径(我的是D: ...

  6. Dynamic Inversions II 逆序数的性质 树状数组求逆序数

    Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...

  7. GCD hdu2588

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. Coin Change (II)(完全背包)

                                                               Coin Change (II) Time Limit: 1000MS   Mem ...

  9. python中如何不区分大小写的判断一个元素是否在一个列表中

    python中判断某一个元素是否在一个列表中,可以使用关键字in 和 not in. 示例如下: 如果需要输出相应的信息,可以搭配使用if语句,这里不赘述. --------------------- ...

  10. android中跨线程向控件传值的问题

    activity.oncreate(bundle savedinstancestate)中创建一个handler类的实例, 在这个handler实例的handlemessage回调函数中调用更新界面显 ...