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]
]

原题链接:https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/

题目:给定一个二叉树,从底到顶返回节点的层序遍历的值。(如,从左到右,一层一层)

思路:上一题的结果上面逆转一下就可以。全然不用出这题吧?

	public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if (root == null)
return list;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (!queue.isEmpty()) {
List<Integer> li = new ArrayList<Integer>();
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
li.add(node.val);
if (node.left != null)
queue.add(node.left);
if (node.right != null)
queue.add(node.right);
}
list.add(li);
}
List<List<Integer>> ret = new ArrayList<List<Integer>>();
for (int i = list.size() - 1; i >= 0; i--) {
ret.add(list.get(i));
}
return ret;
} // Definition for binary tree
public class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
}

LeetCode——Binary Tree Level Order Traversal II的更多相关文章

  1. [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  2. [leetcode]Binary Tree Level Order Traversal II @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...

  3. [Leetcode] Binary tree level order traversal ii二叉树层次遍历

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  4. LeetCode - Binary Tree Level Order Traversal II

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  5. LeetCode "Binary Tree Level Order Traversal II" using DFS

    BFS solution is intuitive - here I will show a DFS based solution: /** * Definition for a binary tre ...

  6. LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)

    题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...

  7. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  8. LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...

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

随机推荐

  1. [转]XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks

    感谢: XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks XNOR-Net ImageNet Cl ...

  2. eclipse里添加类似myeclipse打开当前操作目录

    1.开打eclipse ide,依次run->external tools->external tools configuration 2.在Program下,new一个自己定义的prog ...

  3. 解决WCF 调用方未由服务器进行身份验证或消息包含无效或过期的安全上下文令牌

    错误描述: 1. WCF:调用方未由服务器进行身份验证 2. 无法处理消息.这很可能是因为操作“http://tempuri.org/ISCCLSvc/GetCarriersByWareHouse”不 ...

  4. SQLLoader1(简单测试,以控制文件方式导入数据)

    1.创建表:SQL> conn scott/tiger@orcl;已连接. SQL> CREATE TABLE EMP2 AS SELECT * FROM EMP WHERE 1=2; 表 ...

  5. 纯html网页重定向与跳转

    javaScript 跳转 方法一: <script language="javascript">    window.location = "http:// ...

  6. 探究foreach对于迭代变量的封装性的研究

    众所周知教科书上对于foreach之中的注释是在遍历过程中无法改变其遍历的元素例如声明一个数组 ,,,}; foreach(int m in ii){ m = ;//错误 “m”是一个“foreach ...

  7. FFMPEG视音频解码【一】

    多媒体的时代,得多了解点编解码的技术才行,而ffmpeg为我们提供了一系列多媒体编解码的接口,如何用好这些接口达到自己所需要的目的,这也是一门重要的学问. 要是了解得不够,总是会遇到一堆又一堆问题:网 ...

  8. CMarkUp读写XML(转)

    Fast start to XML in C++ Enough bull. You want to create XML or read and find things in XML. All you ...

  9. UVALive 4119 Always an integer (差分数列,模拟)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Always an integer Time Limit:3000MS     M ...

  10. [转]Python存取XML方法简介

    转自:http://www.cnblogs.com/salomon/archive/2012/05/28/2518648.html 目前而言,Python 3.2存取XML有以下四种方法: 1.Exp ...