leetcode 103
此题难度在于如何标记每一层的末尾节点。
思路1:队列层次遍历,遇到偶数层末尾反转一下数组
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> res;
if(!root) return res;
int t;
vector<int> tmp;
deque<TreeNode*> deq;
deq.push_back(root);
TreeNode* p,*tail=root;//tail记录每一层的最后一个结点
bool isEven=true; //当前遍历到的层数是奇数层,就从左到右遍历
while(!deq.empty()){
p=deq.front();
tmp.push_back(p->val);
deq.pop_front();
if(p->left) deq.push_back(p->left);
if(p->right) deq.push_back(p->right);
if(p==tail){ //遍历完一层
tail=deq.back();
if(!isEven){ //交换vector位置
int l=tmp.size();
for(int i=;i<l/;i++){
t=tmp[l--i];
tmp[l--i]=tmp[i];
tmp[i]=t;
}
}
res.push_back(tmp);
isEven=!isEven;
tmp.clear();
}
}
return res;
}
};
思路2:双栈
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
if(root ==NULL)return {};
stack<TreeNode*> s;
stack<TreeNode*> q;
int flag = ;//使用哪个栈
vector<vector<int>> res;
s.push(root);
while(!s.empty() || !q.empty())
{
if(flag == )
{
vector<int> v_t;
flag = ;
while(!s.empty())
{
TreeNode* tmp = s.top();s.pop();
v_t.push_back(tmp->val);
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
if(v_t.size()>) res.push_back(v_t);
}
else
{
vector<int> v_t;
flag = ;
while(!q.empty())
{
TreeNode* tmp = q.top();q.pop();
v_t.push_back(tmp->val);
if(tmp->right) s.push(tmp->right);
if(tmp->left) s.push(tmp->left);
}
if(v_t.size()>) res.push_back(v_t);
}
}
return res;
}
};
leetcode 103的更多相关文章
- [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)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- leetcode 103二叉树的锯齿形层次遍历
与102相比就增加了flag,用以确定要不要进行reverse操作 reverse:STL公共函数,对于一个有序容器的元素reverse ( s.begin(),s.end() )可以使得容器s的元素 ...
- Java实现 LeetCode 103 二叉树的锯齿形层次遍历
103. 二叉树的锯齿形层次遍历 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null ...
- leetcode 103 Binary Tree Zigzag Level Order Traversal ----- java
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
原题地址 基本数据结构操作,二叉树的层次遍历. 代码: vector<vector<int> > zigzagLevelOrder(TreeNode *root) { vect ...
- [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 _ Medium tag: BFS
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- Java for 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 ...
随机推荐
- Atom以及amWiki个人维基安装设置
amWiki个人维基 amWiki支持.md文件的静态维基系统 安装 参考 安装Atom 下载amWiki解压zip到C:\Users\用户名\.atom\packages 目录下,或在Atom中搜索 ...
- Java基础语法-Unicode、UTF-8、UTF-16
1.Unicode(统一码.万国码),从名字里可以看出,unicode码表囊括世界上各国语言文字. unidode中包含17个代码级别,第一个代码级别又称作基本的多语言级别(码点从U+0000到U+F ...
- Java-对象及变量的并发访问小结
1)多线程环境下,方法内的变量是线程安全的 2)多个线程同时处理一个实例,这个实例内的变量是不安全的 3)不同线程中注入同一个类的不同实例,实例中的变量是安全的 4)Synchronized获取到的锁 ...
- Linux系统下为普通用户加sudo权限
Linux下为普通账号加sudo权限 1. 错误提示:当我们使用sudo命令切换用户的时候可能会遇到提示以下错误:xxx is not in the sudoers file. This incide ...
- pom的maven仓库的配置
这里简单记录一下问题 本人配置了nexus的私人仓库,配置阿里云的远程仓库(http://182.92.29.40/nexus/content/groups/public/)和正规的2个库(http: ...
- JSP等模板引擎已死,大前端为趋势
我们先来看一下tomcat版本数据: 我们可以看到,从tomcat8之后JSP就再没有新的支持了,tomcat8大概是2013年发布的,也就是说,从2013年开始jsp技术已经实质上不被官方继续维护了 ...
- 算法(第四版)C# 习题题解——3.1
写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更方便的版本见:https ...
- Celery完成定时任务
1.什么是Celery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 celery支持linux,如果windows使用celery ...
- Linux 系统调用(system call)
1 系统调用:(SYSTEM CALL) 操作系统(operating system)内核中有一组实现系统功能的过程,系统调用就是对上述过程的调用.程序员利用系统调用,向OS提出服务请求,由OS代为完 ...
- ES6常用语法(下)
Symbol类型 ES5 的对象属性名都是字符串,这容易造成属性名的冲突.比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法,新方法的名字就有可能与现有方法产生冲突.如果有一种机 ...