[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 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],
]
confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".
/**
* Definition for binary tree
* 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>> res;
queue<TreeNode *> Q;
if(root) Q.push(root); while( !Q.empty())
{
int count=;
int levCount=Q.size();
vector<int> levNode; while(count<levCount)
{
TreeNode *curNode=Q.front();
Q.pop();
levNode.push_back(curNode->val);
if(curNode->left)
Q.push(curNode->left);
if(curNode->right)
Q.push(curNode->right);
count++;
}
res.push_back(levNode);
}
reverse(res.begin(),res.end()); //在上一题的基础上增加一行
return res;
}
};
方法二:利用栈
/**
* Definition for binary tree
* 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>> res;
queue<TreeNode *> Q;
if(root) Q.push(root);
stack<vector<int>> stk;
while( !Q.empty())
{
int count=;
int levCount=Q.size();
vector<int> levNode; while(count<levCount)
{
TreeNode *curNode=Q.front();
Q.pop();
levNode.push_back(curNode->val);
if(curNode->left)
Q.push(curNode->left);
if(curNode->right)
Q.push(curNode->right);
count++;
}
stk.push(levNode); //压入栈
}
while(!stk.empty()) //出栈
{
res.push_back(stk.top());
stk.pop();
}
return res;
}
};
[Leetcode] Binary tree level order traversal ii二叉树层次遍历的更多相关文章
- [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 ...
- [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)
题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...
- [leetcode]Binary Tree Level Order Traversal II @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...
- 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 ...
- LeetCode - Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- Binary Tree Level Order Traversal(二叉树广度优先遍历或逐层遍历)
来源:https://leetcode.com/problems/binary-tree-level-order-traversal Given a binary tree, return the l ...
- Binary Tree Level Order Traversal II(层序遍历2)
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- 「日常训练」Equation(HDU-5937)
题意与分析 时隔一个月之后来补题.说写掉的肯定会写掉. 题意是这样的:给1~9这些数字,每个数字有\(X_i\)个,问总共能凑成多少个不同的等式\(A+B=C\)(\(A,B,C\)均为1位,\(1+ ...
- JMeter录制Web脚本
设置Firefox浏览器代理, 点击右上角的菜单: 点击选项: 点击高级: 点击设置: 点击手动配置代理, 输入本地的IP地址和端口号8888,与JMeter代理服务器的端口号保持一致: 好了,浏览器 ...
- linux学习总结----shell编程
## 环境变量 ## 全局变量 ``` 常见的全局环境变量 PATH 指令的搜索路径 HOME 用户的家目录 LOGNAME 登录名 SHELL 脚本的类型 使用全局环境变量 echo $PATH 自 ...
- String和StringBuffer以及StringBuilder的区别
今天在读<java编程思想>的时间,在看到String和StringBuffer以及StringBuffer这三个类的时间,做一个随笔小结,为自己的面试做好准备! 一:String,Str ...
- synchronized 详细解说
转自 http://blog.csdn.net/javazejian/article/details/72828483 出自[zejian的博客] 写的很详细很好,做下记录 本篇主要是对Java并发 ...
- Linux内核设计笔记7——中断
中断与中断处理 何为中断? 一种由设备发向处理器的电信号 中断不与处理器时钟同步,随时可以发生,内核随时可能因为中断到来而被打断. 每一个中断都有唯一一个数字标志,称之为中断线(IRQ) 异常是由软件 ...
- ThinkPHP - 5 - 学习笔记(2015.4.15)
ThinkPHP __construct()和__initialize() 1.__initialize()不是php类中的函数,php类的构造函数只有__construct().2.类的初始化:子类 ...
- 三:Fair Scheduler 公平调度器
参考资料: http://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/FairScheduler.html http://h ...
- OSI七层协议模型及OSI参考模型中的数据封装过程
转载自:http://blog.csdn.net/qq_14935437/article/details/71081546 OSI模型,即开放式通信系统互联参考模型(Open System Inter ...
- CentOS6 安装VNCserver
1.下载vncserver yum install tigervnc tigervnc-server -y 2.配置 vncserver vi /etc/sysconfig/vncserver 在文件 ...