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 1: 普通层次遍历,借助queue

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) { //runtime: 8ms
vector<vector<int>> vec;
if(!root)return vec;
vector<int> v; queue<TreeNode*> q1,q2;
q1.push(root);
while(!q1.empty()){
TreeNode* temp = q1.front();
q1.pop();
v.push_back(temp->val);
if (temp->left)
q2.push(temp->left);
if (temp->right)
q2.push(temp->right); if(q1.empty()){
if (!v.empty())
vec.push_back(v);
v.clear();
swap(q1, q2);
}
}
reverse(vec.begin(),vec.end());    //or vector<vector<int>> ret(vec.rbegin(),vec.rend());return ret;
     return vec;
   }
}

Solution 2: 递归, 待续

【LeetCode】107 - Binary Tree Level Order Traversal II的更多相关文章

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

  2. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

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

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

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

  4. 【easy】107. Binary Tree Level Order Traversal II 按层输出二叉树

    按层输出二叉树,广度优先. 3 / \ 9 20 / \ 15 7 [ [15,7], [9,20], [3] ] /** * Definition for a binary tree node. * ...

  5. 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)

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

  6. 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...

  7. 【LeetCode】102 - Binary Tree Level Order Traversal

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

  8. 【LeetCode】102. Binary Tree Level Order Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...

  9. LeetCode OJ 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. ftp下载显示进度

    经常用到ftp get命令下载东西,但是遇到大的文件不知道是挂了还是在运行,要是能显示就好了,于是就有了下文... 注: 红色 字体是我敲击的命令 “#” 是注释语 [root@localhost s ...

  2. android ADB命令的使用

    http://jingyan.baidu.com/article/fcb5aff7f55c63edab4a7174.html 综上所述,我觉得告知各位菜鸟同学如何删除自带的程序是很有必要的一件事情.1 ...

  3. Case 架构的实际应用-1

    We use testlink to manage cases, and the frame is below: Project Name -All Features(Modules) -Featur ...

  4. Eclipse 下如何删除一个项目的 SVN 信息

    选中项目,右键 - Team - 断开连接 出现如下对话框,根据需要,选择 “删除”或者“不删除”,点击 Yes 即可

  5. [原]HDU-1598-find the most comfortable road(暴力枚举+Kruskal最小生成树)

    题意: 给出一个图,然后Q个询问,每次询问从一个节点到另一个节点,联通图中的“最大边和最小边之差”的最小值,但如果节点之间不连通,则输出-1. 思路:由于询问Q < 11,m < 1000 ...

  6. centos chrome

    在centos6.X和redhat enterprise 中安装chrome,我找了很久都不行,今天终于找到了可以用下脚本那安装: #! /bin/bash # Google Chrome Insta ...

  7. jsp中@import导入外部样式表与link链入外部样式表的区别

    昨天碰到同事问了一个问题,@impor导入外部样式与link链入外部样式的优先级是怎样的,为什么实验的结果是按照样式表导入后的位置来决定优先级.今天就这个问题具体总结如下:   先解释一下网页添加cs ...

  8. 第7篇 ORACLE EBS DEMO虚拟机环境的安装

    ERP信息系统的实施不仅要求懂得道理方面的知识,更要侧重于应用实践.为了有一个稳定的测试环境.初学者可以自己搭建一个EBS DEMO环境.本节介绍EBS DEMO环境虚拟机的安装.一. 安装前的准备( ...

  9. Spring3.1新特性介绍

    Spring3.1新特性 一.Spring2.5之前,我们都是通过实现Controller接口或其实现来定义我们的处理器类.   二.Spring2.5引入注解式处理器支持,通过@Controller ...

  10. 【量化】docker

    查看docker docker ps docker ps -a 删除docker docker stop 8809752ca95a docker rm 8809752ca95a 打包fly cd ~/ ...