题目:

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

代码:

/**
* 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) {
vector<vector<int> > ret;
if (!root) return ret;
vector<int> tmp_ret;
deque<TreeNode *> currLevel, nextLevel;
currLevel.push_back(root);
while ( !currLevel.empty() )
{
while ( !currLevel.empty() )
{
TreeNode * tmp = currLevel.front();
currLevel.pop_front();
tmp_ret.push_back(tmp->val);
if ( tmp->left ) nextLevel.push_back(tmp->left);
if ( tmp->right ) nextLevel.push_back(tmp->right);
}
ret.push_back(tmp_ret);
tmp_ret.clear();
std::swap(currLevel, nextLevel);
}
std::reverse(ret.begin(), ret.end());
return ret;
}
};

tips:

Binary Tree Level Order Traversal的基础上加一个reverse即可。

============================================

第二次过这道题,直接用reverse的路子了。

/**
* 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) {
vector<vector<int> > ret;
queue<TreeNode*> curr;
queue<TreeNode*> next;
if ( root ) curr.push(root);
while ( !curr.empty() )
{
vector<int> tmp;
while ( !curr.empty() )
{
tmp.push_back(curr.front()->val);
if ( curr.front()->left ) next.push(curr.front()->left);
if ( curr.front()->right ) next.push(curr.front()->right);
curr.pop();
}
ret.push_back(tmp);
std::swap(next, curr);
}
std::reverse(ret.begin(), ret.end());
return ret;
}
};

【Binary Tree Level Order Traversal II 】cpp的更多相关文章

  1. 【遍历二叉树】05二叉树的层次遍历II【Binary Tree Level Order Traversal II】

    就把vector改成用栈类存放层次遍历的一层的序列 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  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-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

    [107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...

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

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

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

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

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

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

  9. 63. Binary Tree Level Order Traversal II

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

随机推荐

  1. 在Eclipse中查看JDK类库的源代码

    http://blog.csdn.net/a81895898/article/details/8486802 在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> ...

  2. s3c6410_MMU地址映射过程详述

    参考: 1)<ARM1176 JZF-S Technical Reference Manual>: Chapter 3 System Control Coprocessor Chapter ...

  3. STL使用sort注意的问题

    结构体使用sort算法时,重载operator<(..).如果我们按下面这样写 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  4. Elipse安装Spring Tool Suite

    STS实际上是对Eclipse的Spring包装,下载STS IDE可以直接开发spring web. 但大多数人还是喜欢使用eclipse.下面就eclipse安装sts插件做个介绍. 1.首先到s ...

  5. Spark提交任务到集群

    提交Spark程序到集群与提交MapReduce程序到集群一样,首先要将写好的Spark程序打成jar包,再在Spark-submit下通过命令提交. Step1:打包程序 Intellij IDEA ...

  6. Ajax的get请求向服务器请求数据五步骤?

    如下: ①创建ajax对象 ②建立http请求 ③发送http请求 ④设置ajax对象状态改变的回调函数 ⑤判断ajax状态是否等于4,做相应的业务逻辑

  7. PHP使用phpexcel读取excel文件

    PHP读取excel文件 require("Classes/PHPExcel.php"); require("Classes/PHPExcel/IOFactory.php ...

  8. jquery.tmpl 用法(附上详细案例)

    js的模板引擎就和服务端的差不多,都是更好更快的拼接html用于显示,我参考了文章:http://www.cnblogs.com/zhuzhiyuan/p/3510175.html tmpl常用标签 ...

  9. MongoDB 学习笔记(二)—— MongoDB Shell

    MongoDB自带一个JavaScript shell 可以从命令行中与MongoDB交互,功能非常强大.如在上一节最后一张图所看到,可以执行JavaScript程序. 运行Shell 前提是启动Mo ...

  10. 《Prism 5.0源码走读》Bootstrapper

    Prism框架需要在应用程序启动的时候进行一些初始化的工作,Bootstrapper就是来做这些的,是其切入点. Bootstrapper主要要做的事有:创建和配置module catalog,创建D ...