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. eclipse版本选择

    Eclipse最初是由IBM公司开发的替代商业软件Visual Age for Java的下一代IDE开发环境,2001年11月贡献给开源社区,现在它由非营利软件供应商联盟Eclipse基金会. Ec ...

  2. 小巧玲珑:机器学习届快刀XGBoost的介绍和使用

    欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:张萌 序言 XGBoost效率很高,在Kaggle等诸多比赛中使用广泛,并且取得了不少好成绩.为了让公司的算法工程师,可以更加方便的 ...

  3. 搭建连接MySql的三层架构的ASP.NetCore2.0的WebApi

    里我们用三层架构搭建一个连接MySql的ASP.netCore模板的WebApi项目 首先添加WebApi项目(ASP.NetCore版本) 右键解决方案>新建项目> 选择Web>A ...

  4. geotrellis使用(三十四)矢量瓦片技术研究——矢栅一体化

    前言 本文所涉及技术与Geotrellis并无太大关系,仅是矢量瓦片前端渲染和加载技术,但是其实我这是在为Geotrellis的矢量瓦片做铺垫.很多人可能会说,Geotrellis为什么要搞矢量瓦片, ...

  5. JAVA 并发(待补全!)

    从性能上看 如果没有任务会阻塞 那么在单处理器的机器人使用并发就没有任何意义 (需要上下文切换 时间反而长) 进程是运行在他自己地址空间的自包容的程序 协作多线程与抢占式多线程 想要定义任务需要实现R ...

  6. Invoke 用法

    转自:http://blog.sina.com.cn/s/blog_5a6f39cf0100s23x.html 在多线程编程中,我们经常要在工作线程中去更新界面显示,而在多线程中直接调用界面控件的方法 ...

  7. 在 JavaScript 中使用构造器函数模拟类

    今天,我们要讲的是在 JavaScript 中使用构造器函数(construcor function)模拟类. 构造器函数简介 你可以使用 ES6 的 class 关键字来实现类,不过我建议你使用传统 ...

  8. 关于 char 、 wchar_t 、 TCHAR 、 _T() ||| 宏 _T 、 TEXT 、 _TEXT 、 L

    char :单字节变量类型,最多表示256个字符,wchar_t :宽字节变量类型,用于表示Unicode字符,它实际定义在<string.h>里:typedef unsigned sho ...

  9. bzoj3209 花神的数论题 (二进制数位dp)

    二进制数位dp,就是把原本的数字转化成二进制而以,原来是10进制,现在是二进制来做,没有想像的那么难 不知到自己怎么相出来的...感觉,如果没有一个明确的思路,就算做出来了,也并不能锻炼自己的能力,因 ...

  10. uva11401

    题目大意:计算从1,2,3,...,n中选出3个不同的整数,使得以它们为边长可以构成三角形的个数. 思路:用一般的方法需要三重循环,时间复杂度为O(n^3),肯定超时,因此可用数学的方法对问题进行分析 ...