[Leetcode] Binary tree Zigzag level order traversal二叉树Z形层次遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree{3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
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);
size_t level=; //第level层,初始为第一层
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++;
}
if(level% ==) //偶数层,反转levNode再压入res
reverse(levNode.begin(),levNode.end());
res.push_back(levNode);
level++;
}
return res;
}
};
思路二:
利用队列,在每一层结束时向栈中压入NULL, 则遇到NULL就标志一层的结束,就可以存节点,分奇偶的选择性的反转当前层的节点。特别值得注意的是,循环到最后一行后,若是还压入NULL,会造成死循环,故在压人入NULL时要判断栈是否为空。这也是在
层次遍历为主题的基础上增加层反转条件即可。
/**
* 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> > zigzagLevelOrder(TreeNode *root)
{
vector<vector<int>> res;
queue<TreeNode *> Q;
if(root==NULL) return res;
Q.push(root);
Q.push(NULL); //第一层的结束
vector<int> levNode; //存放每层的节点
size_t level=; //第level层,初始为第一层
while( !Q.empty())
{
TreeNode *cur=Q.front();
Q.pop();
if(cur)
{
levNode.push_back(cur->val); //必须在if内,因栈顶节点可能为NULL
if(cur->left)
Q.push(cur->left);
if(cur->right)
Q.push(cur->right);
}
else
{
if(level% ==) //偶数层,反转levNode再压入res
reverse(levNode.begin(),levNode.end());
res.push_back(levNode);
level++; levNode.clear();
if( !Q.empty()) //最后一行,不要压入
Q.push(NULL);
}
}
return res;
}
};
[Leetcode] Binary tree Zigzag level order traversal二叉树Z形层次遍历的更多相关文章
- [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树Z字形层序遍历
相对于102题,稍微改变下方法就行 迭代方法: 在102题的基础上,加上一个变量来判断是不是需要反转 反转的话,当前list在for循环结束后用collection的反转方法就可以实现反转 递归方法: ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] 103. Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树来回遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [leetcode]Binary Tree Zigzag Level Order Traversal @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: Given a binary tr ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- Binary Tree Zigzag Level Order Traversal(z字形打印二叉树)
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- Binary Tree Zigzag Level Order Traversal,z字形遍历二叉树,得到每层访问的节点值。
问题描述: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from l ...
随机推荐
- Unity调用Android的两个方式:其一、调用jar包
unity在Android端开发的时候,免不了要调用Java:Unity可以通过两种方式来调用Android:一是调用jar.二是调用aar. 这篇文章主要讲解怎么从无到有的生成一个jar包,然后un ...
- Go - 第一个 go 程序 -- helloworld
创建程序目录 接着上一节的内容,在我们的workspace (D:\Gopher) 里面创建子目录 hello,他的绝对路径为:D:\Gopher\src\github.com\tuo\hello 创 ...
- 深入浅出node.js
http://www.infoq.com/cn/articles/what-is-nodejs/
- 在SOUI中支持高分屏显示
和手机屏幕一样,高分屏在PC上使用越来越多.传统的桌面程序都是像素为单位进行UI布局,而且是适配传统的96dpi的显示器的.这就导致这些程序在高分屏上显示很小,用户用起来很难受. 虽然windows系 ...
- ELK logstash 处理MySQL慢查询日志(初步)
写在前面:在做ELK logstash 处理MySQL慢查询日志的时候出现的问题: 1.测试数据库没有慢日志,所以没有日志信息,导致 IP:9200/_plugin/head/界面异常(忽然出现日志数 ...
- hadoop配置文件的作用
core-site.xml <property> //指定hdfs的主端口 namenode要放在哪台机器上 <name>fs.defaultFS</name> & ...
- gitignore.io-程序猿值得拥有的智能生成gitignore文件的秘密武器
gitignore.io Create useful .gitignore files for your project by selecting from 360 Operating System, ...
- 浏览器播放rtsp流媒体解决方案
老板提了一个需求,想让网页上播放景区监控的画面,估计是想让游客达到未临其地,已知其境的状态吧. 说这个之前,还是先说一下什么是rtsp协议吧. RTSP(Real Time Streaming ...
- 大数的加法运算,杭电oj-1002
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1002 [Problem Description] I have a very simple pro ...
- SpringMVC——DispatcherServlet的IoC容器(Web应用的IoC容器的子容器)创建过程
在上一篇<Spring--Web应用中的IoC容器创建(WebApplicationContext根应用上下文的创建过程)>中说到了Web应用中的IoC容器创建过程.这一篇主要讲Sprin ...