Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal is LeetCode 103 (Medium). Given the root of a binary tree, return its values level by level, reading the first level left to right, the next right to left, and so on.
- The tree can be empty; then the answer is
[]. - It has up to 2000 nodes, so one O(n) pass is all that is needed.
- The number of nodes in the tree is in the range [0, 2000].
- -100 <= Node.val <= 100
Intuition
Which nodes sit on a level does not depend on the reading direction; only their order inside the row does. So the zigzag order is an ordinary level-by-level BFS with one change at the end of each level:
- Even levels (0, 2, …): save the row as collected, left to right.
- Odd levels (1, 3, …): reverse the row, then save it.
The queue itself always works left to right, so every level is collected correctly, and only the finished row is flipped. A flag that changes after each level says which case applies.
Any by-level output with a twist, such as zigzag rows, the right side view, level averages or the largest value per row, starts from the same BFS that reads len(queue) at the start of each level. The twist is applied to the finished row, not to the queue.
Approach
Before reading on: for [1,2,3,4,5,6,7] the answer is [[1], [3, 2], [4, 5, 6, 7]]. If you enqueued 3 before 2 to read level 1 backwards, in which order would their children come out on level 2?
Start a queue with the root
Return [] for an empty tree. Otherwise put the root in a queue and set left_to_right = True, because level 0 is read left to right.
Take one whole level
Read len(queue) before the inner loop: that is the number of nodes on the current level. Take exactly that many from the front, append each value to level, and enqueue the node's left child, then its right child. The children belong to the next level and wait behind the current nodes.
Reverse every second row
If left_to_right is false, reverse level. Then append it to the result and flip the flag. Only the finished row is reversed; the queue still holds the next level left to right, so that level is collected correctly too.
Code in Python, C++ and Java
Common pitfalls
Switching the order children are enqueued
if left_to_right:
queue.append(node.right)
queue.append(node.left)
else:
queue.append(node.left)
queue.append(node.right)if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
...
if not left_to_right:
level.reverse()Level 1 comes out right, [3, 2], but then 3's children are queued before 2's, so level 2 is [6, 7, 4, 5] instead of [4, 5, 6, 7]. Swapping the child order only flips each pair of siblings; it does not reverse the whole row.
Not fixing the level size before the loop
while queue:
node = queue.popleft()
level.append(node.val)for _ in range(len(queue)):
node = queue.popleft()
level.append(node.val)Children are added while the level is being read, so a loop that runs until the queue is empty swallows every level into one row. range(len(queue)) is evaluated once, before any child is added.
Complexity
Zigzag Level Order Traversal FAQ
Is GFG ZigZag Tree Traversal the same problem?
Nearly. The GeeksforGeeks version returns one flat list instead of one list per level, so extend a single list with each row. Its older spiral order problem reverses the opposite levels, so check whether the second row in the example is read left to right.
Can each row be built in order without reversing it?
Yes. Create the row with size empty slots and write the i-th node taken to index i on left-to-right levels and to size - 1 - i on the others; a deque with appendleft does the same. Another version keeps two stacks, one per direction. All are O(n), the same as reversing.