【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 ...
随机推荐
- java实现身份证校验
原文来自:java教程网 题目:身份证校验 身份证校验 如果让你设计个程序,用什么变量保存身份证号码呢?长整数可以吗?不可以! 因为有人的身份证最后一位是"X" 实 ...
- CentOS安装中文输入法
yum install "@Chinese Support" System->Preferences->Input Method,勾选"Enable inpu ...
- c/c++基本问题
1. 使用g++将文件编译成库文件 g++ -c -O2 -fPIC test.cpp -o test.o && g++ -shared -Wall -o test.so test.o ...
- 类名 对象名 =new 类名();
类名 对象名 =new 类名();该怎么理解 类名 对象名 =new 类名();=左边:创建一个类的对象=右边:调用这个类的构造函数初始化对象,类名()这个是构造函数,用来做初始化的.
- css style与class之间的区别,cssclass
问题描述: 网页点击[导出]按钮后,将页面table内容另存成excel文件,却发现无法保存表格样式 分析过程: 1.table表格用class,而不是style.导出时并没有导出class定义 ...
- Linux安装VritualBox实现虚拟机win2003端口映射 支持远程
1. 使用VNC登录到Linux系统 2. 安装VritualBox 找到VritualBox的软件包 这里的是run格式的 可以直接在终端运行 需要几分钟时间 3.VritualBox新建虚拟机 ...
- 登录成功返回登录前页面js代码
/*------ setCookie(name,value) -----------*/ function setCookie(name,value) { var Days = 30; //此 coo ...
- ubuntu上搭建vsftpd且通过mysql来管理FTP账号
参考文章:http://wiki.ubuntu.org.cn/Vsftpd%E5%92%8Cmysql%E9%85%8D%E7%BD%AE 请各位先按照这篇文章一步一步操作,我这里是记录一些其间遇到的 ...
- 【PHP】配置环境变量
使用Zend Framework的zf.bat创建项目时,出现如下提示:"php.exe"不是内部或外部命令,也不是可运行的程序或批处理文件. 解决方法: 配置php的环境变量: ...
- Java实现0~100之和
经典问题了,三个变量分别表示起始.结尾以及和,for循环从起始到结尾,和不断累积.代码如下: public class ForLoop { public static void main(String ...