【Binary Tree Level Order Traversal II 】cpp
题目:
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的更多相关文章
- 【遍历二叉树】05二叉树的层次遍历II【Binary Tree Level Order Traversal II】
就把vector改成用栈类存放层次遍历的一层的序列 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- 【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 ...
- 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】
[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...
- 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 ...
- 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 ...
- 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 ...
- 102/107. Binary Tree Level Order Traversal/II
原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...
- 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 ...
- 63. Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...
随机推荐
- 第一章 Collections 类、泛型类和Timing类概述
摘抄<数据结构与算法(C#语言描述)> 删除很多废话 1.1群集(collection)的定义 群集是一种结构化的数据类型.存储数据,并且提供数据的添.删.改操作,以及对群集不同属性值的设 ...
- Windows2008下搭建NFS实现windows空间提供linux使用
我们既然是要把Windows 的硬盘给Linux使用 就需要在Windows上安装NFS网络文件系统 操作如下 1.添加角色-----文件服务---勾选网络文件服务 2.安装完成后 我们需要把这30 ...
- C#winform设置DateTimePicker的时间格式
在对DateTimePicker进行时间格式设置时候,要先对属性Format设置为"Custom"自定义格式,然后再CustomFormat里面进行格式设置 比如"yyy ...
- php简单缓存类
<?phpclass Cache { private $cache_path;//path for the cache private $cache_expire;//seconds ...
- 短信接口调用以及ajax发送短信接口实现以及前端样式
我们短信api用的是云信使平台提供的非免费短信服务:官网提供的demo有两种,分别是function加其调用.class文件加其调用. 在这里我们用class文件加调用: 首先,ThinkPHP里面自 ...
- 重拾C,一天一点点_8
这两天发现一个问题,我最近发的几篇博文稀里糊涂地被转到别的网站去了,目前发现有两个网站转载了,一个注明了作者出处(博客园 lltong),但没给任何链接.另一个网站呢,就是直接抓的,而且还抓的乱七八糟 ...
- 发布阿里云OSS for phpcmsV9整合教程
说明:这个算不上是插件,因为没有安装包,需要手工修改代码. 还有一点就是后台发布文章时上传的附件还是会保存在你的服务器上,基于以下原因: 1.个人的需求是前台页面需要使用thumb函数生成多个缩略图大 ...
- 解析 this.initialize.apply(this, arguments)
一. 起因 那天用到prototype.js于是打开看看,才看几行就满头雾水,原因是对js的面向对象不是很熟悉,于是百度+google了一把,最后终于算小有收获,写此纪念一下^_^. prototyp ...
- Linux下自带的regex
Linux下可直接用regex.h来支持正则表达式. Android同样也有该头文件,可认为Android也是支持的. #include <sys/types.h> #include &l ...
- Unity Scripting Tutorials 要点记录
(搬运自我在SegmentFault的博客) 这几天通过Unity官网的Unity Scripting Tutorials的视频学习Unity脚本,观看的过程中做了记录.现在,整理了一下笔记,供自己以 ...