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. 关于APP在小米5s第一次安装启动后,点击home返回桌面,再次进入重进闪屏页问题

    现象 今天工作中,在对公司产品进行测试的时候,程序员小哥点出了一个问题.问题点出的步骤是这样的: 1.安装APP 2.点击打开 3.经过闪屏页,进入主页后,点击HOME键 4.再次进入程序会重新进入闪 ...

  2. Sql Server——运用代码创建数据库及约束

    在没有学习运用代码创建数据库.表和约束之前,我们只能用鼠标点击操作,这样看起来就不那么直观(高大上)了. 在写代码前要知道在哪里写和怎么运行: 点击新建查询,然后中间的白色空白地方就是写代码的地方了. ...

  3. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  4. 如何用kaldi做孤立词识别-初版

    ---------------------------------------------------------------------------------------------------- ...

  5. 一次生产环境下MongoDB备份还原数据

    最近开发一个版本的功能当中用到了MongoDB分页,懒于造数据,于是就研究了下从生产环境上导出数据到本地来进行测试. 研究了一下,发现MongoDB的备份还原和MySQL语法还挺类似,下面请看详细介绍 ...

  6. Even Parity uva11464 模拟

    Even Parity Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   ...

  7. hdu4027 开方,记录

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...

  8. 最长上升子序列 LIS(Longest Increasing Subsequence)

    引出: 问题描述:给出一个序列a1,a2,a3,a4,a5,a6,a7….an,求它的一个子序列(设为s1,s2,…sn),使得这个子序列满足这样的性质,s1<s2<s3<…< ...

  9. Android02-控件

    在android studio中,新建一个module时布局文件中就会默认带一个TextView,里面显示着一句话:Hello World !  布局中通常放置的是android控件,下面介绍几个an ...

  10. docker命令不需要敲sudo的方法

    由于docker daemon需要绑定到主机的Unix socket而不是普通的TCP端口,而Unix socket的属主为root用户,所以其他用户只有在命令前添加sudo选项才能执行相关操作. 如 ...