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. Map.containsKey方法——判断Map集合对象中是否包含指定的键名

    该方法判断Map集合对象中是否包含指定的键名.如果Map集合中包含指定的键名,则返回true,否则返回false. public static void main(String[] args) { M ...

  2. pygame 精灵的行走及二段跳实现方法

    不得不承认<Python游戏编程入门>这本书翻译.排版非常之烂,但是里面的demo还是很好的,之前做了些改编放到这里. 先是素材: 背景 精灵 所有素材均取自此书 接下来就是精灵类的创建了 ...

  3. oracle 删除外键约束 禁用约束 启用约束

    oracle 删除外键约束 禁用约束 启用约束 执行以下sql生成的语句即可 删除所有外键约束 Sql代码  select 'alter table '||table_name||' drop con ...

  4. Git的使用详解

    起步 关于版本控制 Git 简史 Git 基础 安装 Git 初次运行 Git 前的配置 获取帮助 小结 Git 基础 取得项目的 Git 仓库 记录每次更新到仓库 查看提交历史 撤消操作 远程仓库的 ...

  5. RocketMQ之双Master方式部署以及简单使用

    1.1.服务器环境 192.168.100.24 root nameServer1,brokerServer1 Master1 192.168.100.25 root nameServer2,brok ...

  6. JAVA数据流再传递

    有一个filter类,在请求进入的时候读取了URL信息,并且读取了requestBod中的参数信息,那么在请求到达实际的控制层时,入参信息是拿不到的,对这种情况就需要数据流做再传递处理. 处理原理:使 ...

  7. 【Python学习笔记之一】Python关键字及其总结

    前言 最近在学习Java Sockst的时候遇到了一些麻烦事,我觉得我很有必要重新研究学习Python这种脚本语言,参考大神的经验,淘到了一本学习Python的好书<"笨方法" ...

  8. ngRepeat track by

    刚刚看见一篇文章讲述track by的功能的,大致记录如下: 1. ng-repeat="friend in friends" 一般不使用track by的情况下,每次刷新DOM, ...

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

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

  10. SqlHelper工具类

    public class SqlHlper { public static readonly string constr = ConfigurationManager.ConnectionString ...