此题难度在于如何标记每一层的末尾节点。

思路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的更多相关文章

  1. [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 ...

  2. LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

    103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...

  3. leetcode 103二叉树的锯齿形层次遍历

    与102相比就增加了flag,用以确定要不要进行reverse操作 reverse:STL公共函数,对于一个有序容器的元素reverse ( s.begin(),s.end() )可以使得容器s的元素 ...

  4. Java实现 LeetCode 103 二叉树的锯齿形层次遍历

    103. 二叉树的锯齿形层次遍历 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null ...

  5. 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 ...

  6. Leetcode#103 Binary Tree Zigzag Level Order Traversal

    原题地址 基本数据结构操作,二叉树的层次遍历. 代码: vector<vector<int> > zigzagLevelOrder(TreeNode *root) { vect ...

  7. [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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. IO字节流概念

    1.输入和输出概念: 输入:硬盘到内存为了使用: 输出:内存到硬盘为了保存: 2.一切皆为字节: 计算机只识别二进制数字,一个字节为8个二进制数字: 存储在硬盘是字节,传输也是字节:

  2. java面试经常问到的计算机网络问题

    GET 和 POST 的区别 GET请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的:/test/demo_form.asp?name1=value1&name2=val ...

  3. Web 前端编程运维必备

    Html 1.Html 标签初知 2.Html 标签种类 3.Html 符号 4.Html Title 标签 5.Html meta 标签 6.Html Link 标签 7.Html p 标签 8.H ...

  4. win7有多条隧道适配器(isatap、teredo、6to4)的原因及关闭方法

    问题:sdp协商时,带有IPV6的信息,需要将IPV6相关信息去掉 原因:网卡启用了ipv6通道 解决:关闭IPv6数据接口 netsh interface isatap set state disa ...

  5. Bugku-CTF之速度要快

    Day21 速度要快 速度要快!!!!!! http://123.206.87.240:8002/web6/

  6. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  7. Python3 tkinter基础 Tk quit 点击按钮退出窗体

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. 配置TortoiseGit与Github

    https://jingyan.baidu.com/article/495ba841f2892638b30edefa.html https://www.cnblogs.com/maojunyi/p/7 ...

  9. 为 pip install 设置 socks5 代理

    参考 How to use pip with socks proxy? 为 pip install 设置 socks5 代理 设置方法: pip install pysocks pip install ...

  10. 自动化pip安装

    其实正确安装python3.6后,在安装目录里就有pip.exe文件,只不过用的时候,要进入pip的安装目录下进行安装numpy等. 如进入这个目录, D:\Program Files\Python\ ...