Problem description:

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

1
2
3
4
5
  3
/ \
9 20
/ \
15 7

return its zigzag level order traversal as:

1
2
3
4
5
[
[3],
[20,9],
[15,7]
]

Solution1 BFS+ Deque:

Use a deque to store the elements. The idea is to change direction of push elements into the deque and pop elements from deque.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
q, res, d = deque([]), [], True
# True: look from right to left
# False: look from left to right
q.append(root)
while q:
pre_level_size = len(q)
level = []
for _ in range(pre_level_size):
if d:
# cur level pop from right, append next level from left
# pop from right so we would the rightest node as first one
tmp = q.pop()
if tmp.left: q.appendleft(tmp.left)
if tmp.right: q.appendleft(tmp.right)
else:
# cur level pop from left, append next level from right
# pop from left so we would the leftest node as first one
tmp = q.popleft()
if tmp.right: q.append(tmp.right)
if tmp.left: q.append(tmp.left)
level.append(tmp.val)
d = not d
res.append(level)
return res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Definition for a binary tree node.
* 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) return res;
deque<TreeNode*> deq;
bool dir= false;
deq.push_front(root);
while(!deq.empty()){
int size= deq.size();
vector<int> curlevel;
for(int i= 0; i< size; i++){
if(dir){
auto tmp= deq.front(); deq.pop_front();
curlevel.push_back(tmp->val);
if(tmp->right) deq.push_back(tmp->right);
if(tmp->left) deq.push_back(tmp->left);
}
else{
auto tmp= deq.back(); deq.pop_back();
curlevel.push_back(tmp->val);
if(tmp->left) deq.push_front(tmp->left);
if(tmp->right) deq.push_front(tmp->right);
}
}
dir = dir^1;
res.push_back(curlevel);
}
return res;
}
};

time complexity: $O(n)$
space complexity: $O(n)$

Solution2 BFS+ two stacks:

Use two stacks, one for odd layer, the other one for even layer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Definition for a binary tree node.
* 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) return res;
stack<TreeNode*> odd, even;
odd.push(root);

while(!odd.empty() || !even.empty()){
vector<int> level_odd;
while(!odd.empty()){
auto tmp= odd.top(); odd.pop();
if(tmp->left) even.push(tmp->left);
if(tmp->right) even.push(tmp->right);
level_odd.push_back(tmp->val);
}
if(!level_odd.empty()) res.push_back(level_odd);
vector<int> level_even;
while(!even.empty()){
auto tmp= even.top(); even.pop();
if(tmp->right) odd.push(tmp->right);
if(tmp->left) odd.push(tmp->left);
level_even.push_back(tmp->val);
}
if(!level_even.empty()) res.push_back(level_even);
}
return res;
}
};

time complexity: $O(n)$
space complexity: $O(n)$
reference: