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]
]

题目的意思非常直白。层序遍历整个树,可是第一层正序输出。第二层反序输出,第三层正序输出,以此类推。做法有两种:一、仍然採用level-travel,仅仅是引入一个标记,推断是否反转得到的数列; 二、考虑到stack的特点,利用stack FILO的特点来直接输出。两种方法都贴出来

利用stack的:

class Solution {
public:
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
bool isRe = false;
vector<int> a;
stack<TreeNode *> s1, s2; if (root == NULL)
return ret;
s1.push(root);
while (!s1.empty()){
TreeNode *tmp = s1.top();
s1.pop();
a.push_back(tmp->val); if (isRe){
if (tmp->right)
s2.push(tmp->right);
if (tmp->left)
s2.push(tmp->left);
}
else{
if (tmp->left)
s2.push(tmp->left);
if (tmp->right)
s2.push(tmp->right);
}
if (s1.empty()){
ret.push_back(a);
isRe = !isRe;
swap(s1, s2);
a.clear();
}
}
return ret;
}
private:
vector<vector<int>> ret;
};

利用queue的,这里因为引入了swap,所以能够复用同一个代码流程,代码会短一些;

class Solution {
public:
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<vector<int>> ret;
queue<TreeNode *> current, next; //利用两个队列的交替来区分每一层
bool isRe = false;
vector<int> v; if (root == NULL)
return ret;
current.push(root);
while (!current.empty()){
TreeNode *tmp = current.front();
current.pop();
v.push_back(tmp->val); if (tmp->left)
next.push(tmp->left);
if (tmp->right)
next.push(tmp->right); if(current.empty()){
if (isRe){
reverse(v.begin(), v.end());
}
ret.push_back(v);
swap(current,next);
isRe = !isRe;
v.clear();
}
} }
};

LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]的更多相关文章

  1. 103. Binary Tree Zigzag Level Order Traversal (Tree, Queue; BFS)

    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 (swift) 二叉树Z字形层次遍历

    // 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...

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

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

  4. 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告

    Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...

  5. 【leetcode】Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  6. Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树

    题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...

  7. 【LeetCode】103. Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  8. [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  9. LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

    1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...

随机推荐

  1. 图解C#_事件

    概述 今天用来演示事件的例子是模拟实现一个文件下载类,在这个类中我将定义一个DownLoad事件,这个事件用来在文件下载的过程中,向订阅这个事件的用户发出消息,而这个消息将用DownLoadEvent ...

  2. maven生成war包的两种方式

    war包即对WEB应用程序进行打包,用于应用容器的部署.如在jboss中只要把war包丢入deploy目录下即可发布自己的应用了.打包方式有很多中,很多工具本身就支持此功能.下面主要介绍通过maven ...

  3. 基于visual Studio2013解决面试题之0703翻转栈

     题目

  4. linux下C/C++IDE比较——Code::Blocks

    工欲善其事,必先利其器.用了这么久的linux,现在比较主流的几个C/C++的IDE基本已都用过了,现在来对他们做一下简单的比较. 1.VIM首先要说的是VIM.我认为,VIM只是一个编辑器,不能算是 ...

  5. java反射小样例

    package reflect; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExc ...

  6. 第五天学习内容 for循环,嵌套

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. JSP的学习(5)——语法知识三之include指令

    本篇继续来对JSP语法中的JSP指令进行学习,在<JSP的学习(3)——语法知识二之page指令>中,已经介绍了JSP指令的书写格式和page指令的详细信息,所以在这一篇中我们会对JSP指 ...

  8. 14.8.3 Identifying the File Format in Use 确认使用的文件格式;

    14.8.3 Identifying the File Format in Use 确认使用的文件格式: 如果 你启用一个不同的文件格式使用 innodb_file_format configurat ...

  9. Boost Thread学习笔记三

    下面先对condition_impl进行简要分析.condition_impl在其构造函数中会创建两个Semaphore(信号量):m_gate.m_queue,及一个Mutex(互斥体,跟boost ...

  10. cpp check 分析

    1 FileTabCharacterCheck 为什么检查: 因为对于一个TAB而言,所空的空格不定是固定的,如果在机器A上设置了是4个空格,显示正常,而在机器B上阅读,B机器是100个空格为一个TA ...