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.

 /**
* 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>> list = new LinkedList<List<Integer>>();
if(root == null) return list; Queue<TreeNode> queue1 = new LinkedList<TreeNode>();
Queue<TreeNode> queue2 = new LinkedList<TreeNode>(); queue1.offer(root); while(queue1.size()>0){
List<Integer> childlist = new ArrayList<Integer>();
while(queue1.size()>0){
TreeNode node = queue1.remove();
childlist.add(node.val);
if(node.left!=null) queue2.offer(node.left);
if(node.right!=null) queue2.offer(node.right);
}
list.add(0, childlist);
Queue<TreeNode> temp = queue1;
queue1 = queue2;
queue2 = temp;
} return list;
}
}

LeetCode OJ 107. Binary Tree Level Order Traversal II的更多相关文章

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

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

  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 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  4. 【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. LeetCode OJ: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 OJ】Binary Tree Level Order Traversal II

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...

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

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

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

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

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

随机推荐

  1. __stack_chk_fail栈检查失败

    1. __stack_chk_fail的作用 在了函数的局部变量和保存的指令指针(译注:此处指返回地址和EBP)之间.这个值被称作金丝雀(“canary”)值 参考 http://www.freebu ...

  2. Design Pattern——Factory_DP

    namespace TEST { //用于生成一个对应的操作类,这个工厂只是用来产生操作类的,不做其他只用 public class Factory { public Operate GetOpetr ...

  3. HDU-1757--A Simple Math Problem(矩阵乘法)

    Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x > ...

  4. Oracle数据库初探

    一.安装oracle数据库 步骤:转载一个很不错的文档:http://www.linuxidc.com/Linux/2015-02/113222.htm 注意点:安装的时候会check相关依赖,有些可 ...

  5. openstack私有云布署实践【16.1 CentOS7.1 只有根分区镜像制作】

    之所以要只有根分区镜像,是因为在创建VM或者调整云主机的硬盘大小时,它能自动扩容.无需人工介入   在原来的物理机10.40.41.1的CentOS 6.7上制作镜像.(当然如果你的制作镜像的宿主机是 ...

  6. 纯 CSS 实现三角形尖角箭头的实例

    上次无意中发现了个使用纯 CSS 实现三角形尖角箭头的方法 http://blog.csdn.net/zhouzme/article/details/18901943 ,但没有怎么用上,也没有详细完整 ...

  7. get获取Json

    前端代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <sc ...

  8. js--学习方法之-转

    既然你找到这篇文章来,说明你是真心想学好JavaScript的.你没有想错,当今如果要开发现代网站或web应用(包括互联网创业),都要学会JavaScript.而面对泛滥的JavaScript在线学习 ...

  9. <? extends T>和<? super T>

    转自:Java泛型中extends和super的区别? 另,问题来源:Java 泛型 <? super T> 中 super 怎么 理解?与 extends 有何不同? <? ext ...

  10. C++静态成员

    类中的静态成员真是个让人爱恨交加的特性.我决定好好总结一下静态类成员的知识点,以便自己在以后面试中,在此类问题上不在被动. 静态类成员包括静态数据成员和静态函数成员两部分. 一 静态数据成员: 类体中 ...