Question

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

Solution

Same method as "Binary Tree Level Order Traversal". Note that for ArrayList, it has function add(int index, E element).

 /**
* 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>> result = new ArrayList<List<Integer>>();
if (root == null)
return result;
Deque<TreeNode> prev = new ArrayDeque<TreeNode>();
Deque<TreeNode> current;
TreeNode tmpNode;
prev.addLast(root);
while (prev.size() > 0) {
current = new ArrayDeque<TreeNode>();
List<Integer> tmpList = new ArrayList<Integer>();
while (prev.size() > 0) {
tmpNode = prev.pop();
if (tmpNode.left != null)
current.addLast(tmpNode.left);
if (tmpNode.right != null)
current.addLast(tmpNode.right);
tmpList.add(tmpNode.val);
}
prev = current;
result.add(0, tmpList);
}
return result;
}
}

Binary Tree Level Order Traversal II 解答的更多相关文章

  1. 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...

  2. Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...

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

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

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

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

  6. LeetCode_107. Binary Tree Level Order Traversal II

    107. Binary Tree Level Order Traversal II Easy Given a binary tree, return the bottom-up level order ...

  7. 63. Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...

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

  9. 【Binary Tree Level Order Traversal II 】cpp

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

随机推荐

  1. Longest Common Prefix 解答

    Question Write a function to find the longest common prefix string amongst an array of strings. Solu ...

  2. Android 之 Eclipse 导入 Android 源码

    很多人都下载过下图中的 Sources for Android SDK,但是很少人知道怎么用       下载完毕后可以再 Android SDK 根目录下看到 sources 文件夹内 有 andr ...

  3. Hibernate框架大配置关联小配置

    1 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-// ...

  4. 动态载入TreeView时让TreeView节点前显示加号

    解释下标题,我这里通过webservice获取数据并动态载入TreeView节点.那么某个节点展开前它是没有子节点的.那么它就不显示加号.这样会让用户误以为此节点不能展开.我是这样做的,每次创建节点a ...

  5. iOS:UI系列之UINavigationController

    又到了总结的时间了,突然间感觉时间过得好快啊, 总觉的时间不够用,但是这也没办法啊, 只有自己挤时间了,虽然是零基础,但是这并不能代表什么啦,只要努力,收获总还是有的, 同时我也相信广大的博友肯定也有 ...

  6. HTML基础知识笔记(二)

    HTML <img>标签 语法: <img src="图片地址" alt="下载失败时的替换文本" title = "提示文本&qu ...

  7. 限制oracle用户创建、删除、修改用户对象

    在sys用户下执行: CREATE OR REPLACE TRIGGER lms2014BEFORE create or DROP OR ALTER ON databaseDECLAREBEGINIF ...

  8. node.js如何使用回调

    Node.js到处使用回调,尤其在有I/O(输入/输出)操作的地方. 下面是在一个Node.js中使用filesystem模块中从磁盘上读入文件内容示例一: var fs = require('fs' ...

  9. 跨平台渲染框架尝试 - GPU Buffer的管理(1)

    buffer资源 下面来谈谈buffer的管理.buffer资源从广义上就是C语言的数组.如下图所示. 图 buffer的广义模型 在渲染管线中,无论是opengl还是dx或者其他的渲染api,都会提 ...

  10. hdu3830 (二分+LCA)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Checkers Time Limit: 2000/1000 MS (Java/O ...