# 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 right, then right to left for the next level and alternate between).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9  20
/  \
15   7


return its zigzag level order traversal as:

[
[3],
[20,9],
[15,7]
]

class Solution {
public:
/**
* @param root
* @return
*/
vector<vector<int>> zigzagLevelOrder(TreeNode *root) {
vector<vector<int>> ans;
if (root) que.push(root);
int cur = 0;
int flag = 1;
while (cur < que.size()) {
int last = que.size();
vector<int> vec;
flag += 1;
while (cur++ < last) {
TreeNode *tmp = que.front();
vec.push_back(tmp->val);
que.pop();
if (tmp->left)que.push(tmp->left);
if (tmp->right) que.push(tmp->right);
}
cur = 0;
if (flag & 1) reverse(vec.begin(),vec.end());
ans.push_back(vec);
}
return ans;
}

private:
queue<TreeNode *> que;
};


#### LeetCode 103:Binary Tree Zigzag Level Order Traversal

2015-05-31 00:34:47

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

2016-05-28 12:04:05

#### LeetCode 103. Binary Tree Zigzag Level Order Traversal（二叉树之字形遍历）

2016-05-23 03:58:09

#### [Leetcode] 103. Binary Tree Zigzag Level Order Traversal 解题报告

2017-04-24 09:39:20

#### C++详解Leetcode:103. Binary Tree Zigzag Level Order Traversal

2017-07-30 17:22:04

#### LeetCode103 BinaryTreeZigzagLevelOrderTraversal(二叉树Z形层次遍历) Java题解

2015-07-09 17:08:19

#### LeetCode 103 Binary Tree Zigzag Level Order Traversal

2014-08-14 16:11:53

#### leetcode 103: Binary Tree Zigzag Level Order Traversal

2015-08-12 13:00:53

#### leetcode 103 Binary Tree Zigzag Level Order Traversal

2016-06-05 09:22:00

#### LeetCode 103: Binary Tree Zigzag Level Order Traversal

2015-10-27 23:10:32