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

confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
/ \
2 3
/
4
\
5

The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".

PS:二叉树分层输出,BFS并且记录翻转情况
 
 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<vector<int>> res;
if(root==NULL) return res;
queue<TreeNode*> q;
q.push(root);
bool reverse=false;
while(!q.empty()){
vector<int> v;
int size=q.size();
for(int i=;i<size;++i){
TreeNode *cur=q.front();
q.pop();
v.push_back(cur->val);
if(cur->left!=NULL) q.push(cur->left);
if(cur->right!=NULL) q.push(cur->right);
}
if(reverse){ vector<int> tmp;
for(int i=v.size()-;i>=;--i){
tmp.push_back(v[i]);
}
res.push_back(tmp);
}else{
res.push_back(v);
}
reverse=!reverse;
}
return res;
}
};

binary-tree-zigzag-level-order-traversal——二叉树分层输出的更多相关文章

  1. [LeetCode] 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 二叉树的之字形层序遍历

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  3. [Leetcode] Binary tree Zigzag level order traversal二叉树Z形层次遍历

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

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

  5. 103 Binary Tree Zigzag Level Order Traversal 二叉树的锯齿形层次遍历

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

  6. Leetcode103. Binary Tree Zigzag Level Order Traversal二叉树的锯齿形层次遍历

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

  7. [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树Z字形层序遍历

    相对于102题,稍微改变下方法就行 迭代方法: 在102题的基础上,加上一个变量来判断是不是需要反转 反转的话,当前list在for循环结束后用collection的反转方法就可以实现反转 递归方法: ...

  8. 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)

    从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...

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

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

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

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

随机推荐

  1. 关于Android应用中图片占用内存浅谈

    从事过移动端应用开发的童鞋应该都清楚,内存是非常宝贵的资源.如果能很好的利用有限的内存,对应用性能的提升会有很大的帮助.在实际应用开发中图片内存占整个应用非常大的比重,我们只有了解图片是如何加载到内存 ...

  2. C#中找不到MouseWheel事件的解决办法

    在.....Designer.cs中加入 this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this. ...

  3. Educational Codeforces Round 8 B 找规律

    B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. 【08】Vue 之 vue-cli

    8.1. 前置知识学习 npm 学习 官方文档 推荐资料 npm入门 npm介绍 需要了解的知识点 package.json 文件相关配置选项 npm 本地安装.全局安装.本地开发安装等区别及相关命令 ...

  5. JAVA神操作--使用Arthas线上热更新实战

    热更不规范,同事两行泪 背景 C君是一个javaer,最近在开发用户登出接口的时候,不小心把接口参数拼错了 正确的是: /api/v1/user/logout?referrer=www.javaer. ...

  6. [bzoj 1782] [Usaco2010 Feb]slowdown慢慢游

    [bzoj 1782] [Usaco2010 Feb]slowdown慢慢游 Description 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1-N)从 ...

  7. MVC3 中使用 Ajax.ActionLink Ajax.BeginForm

    原文发布时间为:2011-05-01 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/dd381533%28VS.98% ...

  8. .NET泛型编程 性能提升工具 List<T>

    原文发布时间为:2009-10-27 -- 来源于本人的百度文章 [由搬家工具导入] 结论  .NET 2.0中的泛型是强有力的,你写的代码不必限定于一特定类型,然而你的代码却能具有类型安全性。泛型的 ...

  9. why not ovp protection ?

    HW MSM8917 PM8937 PMI8940 Question : Recently, I connect usb cable with 10V to the phone. Why does t ...

  10. 【linux高级程序设计】(第七章)终端及串口编程 未完成

    一.端口设备类型 1.显示设备基本信息 cat /proc/tty/drivers 里面包括了: 当前终端:/dev/tty 前台控制台终端:/dev/console 用于创建虚拟终端的:/dev/p ...