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. 将网站部署到服务器上出现_STORAGE_WRITE_ERROR_问题

    用的thinkphp3.2的框架,在本地运行没有问题,部署到服务器上(基于centos的LAMP环境)即报错,报错信息如下(完全看不懂...):求大神帮帮忙~~~~(>_<)~~~~ :( ...

  2. Redis + Jedis + Spring整合遇到的异常(转)

    项目中需要用到缓存,经过比较后,选择了redis,客户端使用jedis连接,也使用到了spring提供的spring-data-redis.配置正确后启动tomcat,发现如下异常: Caused b ...

  3. 程序中非action获得spring容器

    java类: public class MySpringContext implements ApplicationContextAware{ private static ApplicationCo ...

  4. 某公司ASP.NET应聘上机试题

    ASP.NET笔试题是ASP.NET程序员面试必须经历的,一般会叫你填两个表 1个是你的详细信息表 1个是面试题答卷 两个都要注意反正面是否都有内容不要遗漏,如果考你机试一般也有两种,就是程序连接数据 ...

  5. zipalign 文件路径问题

    在使用zipalign,对Android程序进行打包,有些时候可能提示找不到zipalign ,可以复制一份放在相应的文件夹就行了 windows: 如果缺少zipalign,在网上找到相应的文件放在 ...

  6. 遇到Audio/Speech相关问题,如何抓取log

      [DESCRIPTION] 遇到Audio/Speech相关问题时,经常需要抓取相关log信息,总结抓取方法如下 [SOLUTION] 1.    通话声音相关的问题: Case 1: 通话中某一 ...

  7. Q10Ⅱ 双核 - 产品中心 - 海美迪

    海美迪Q系列视频文明书 Q10Ⅱ 双核 - 产品中心 - 海美迪

  8. 通过OpenSSL解码X509证书文件

    在Windows平台下.假设要解析一个X509证书文件,最直接的办法是使用微软的CryptoAPI. 可是在非Windows平台下,就仅仅能使用强大的开源跨平台库OpenSSL了.一个X509证书通过 ...

  9. IE浏览器上传文件时本地路径变成”C:\fakepath\”的问题

    在使用<input id="file_upl" type="file" />控件上传文件时,有时会需要获取文件本地路径展示给客户,这时可以通过这样的 ...

  10. uva 357 Let Me Count The Ways(01背包)

    题目连接:357 - Let Me Count The Ways 题目大意:有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 解题思路:和uva674是一 ...