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

思路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. px和em的区别, css权重

    PX特点:px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的. EM特点 1. em的值并不是固定的:2. em会继承父级元素的字体大小. 优先级:!important> ...

  2. 用AOP记录操作日志,并写进数据库。

    先用AOP注解 1 package com.vlandc.oss.apigate.log.aspect; import java.util.Map; import java.util.Optional ...

  3. 词云(wordcloud2.js js2wordcloud.js)

    npm安装: npm install js2wordcloud --save 用法 var wc = new Js2WordCloud(document.getElementById('contain ...

  4. 火狐开发----Web开发者工具

    作为开发Web相关人员,有必要了解这个开发工具,会提供给你不少的帮助,进入正题.经典的F12启动,这个大家都知道. 一 控制台可以很好的帮我们掌控错误,包括文件的加载.JS语法.CSS语法.安全问题. ...

  5. java0429 wen 数据库

  6. webpack的常识概念

    它的优势: 递归解析依赖,支持支持es module规范.commonJS.AMD规范. 支持代码分割. loader: css-loader\style-loader\less-loader\sas ...

  7. 连接centos服务器gui

    https://blog.csdn.net/jack_nichao/article/details/78289398 配置好后下载vnc viewer 进行连接. Ubuntu:https://www ...

  8. USACO比赛题泛刷

    随时可能弃坑. 因为不知道最近要刷啥所以就决定刷下usaco. 优先级排在学习新算法和打比赛之后. 仅有一句话题解.难一点的可能有代码. 优先级是Gold>Silver.Platinum刷不动. ...

  9. css浮动与清除浮动

    css浮动 首先,我们要知道,css中块级元素在页面中是独占一行的,自上而下排列,也就是我们所说的流,通常称为标准流. 以div为例,div是块级元素,如下: 可以清楚地看到,div是独占一行的,di ...

  10. Java基础知识盘点(一)- 基础篇

    基本功 面向对象特征 封装.继承.多态和抽象 1.封装:给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法,来改变它内部的数据. 在Java中,其访问权限有3种修饰符:publi ...