Skip to content
AI360Xpert
Beta
Difficulty: MediumTrees

Binary Tree Zigzag Level Order Traversal

Problem in Plain English

Given the root of a binary tree, return its values level by level while alternating direction on every level: even levels run left to right and odd levels run right to left. An empty tree returns an empty list.

Problem Statement

Given the root of a binary tree, return the zigzag level order traversal of its node values.

Level 0 is read left to right, level 1 is read right to left, level 2 is read left to right again, and the direction keeps alternating. Return one list per level, or an empty list when root is null.

Constraints

  • The tree contains between 0 and 2000 nodes, inclusive.
  • Each node value is between -100 and 100, inclusive.

Examples

Example 1

Input
root = [3,9,20,null,null,15,7]
Output
[[3],[20,9],[15,7]]

Level 0 gives [3], level 1 is reversed to [20,9], and level 2 stays [15,7].

Example 2

Input
root = [1]
Output
[[1]]

A single node forms one level read left to right.

Example 3

Input
root = []
Output
[]

An empty tree has no levels, so the answer is an empty list.

Example 4

Input
root = [1,2,3,null,4,5,6]
Output
[[1],[3,2],[4,5,6]]

Reverse the second level to [3,2], but keep discovering children through parents 2 then 3. This makes the third level [4,5,6], rather than [5,6,4]. Missing children do not add output placeholders.

Intuition

Separate which nodes belong to a depth from which direction their values are displayed. A queue discovers each depth left to right; only the output changes direction. In Example 1, visit 9 before 20 to discover their children correctly, but display that level as [20,9]. The next level still displays [15,7].

Approaches

BFS Then Reverse

Solution Details

Reveal BFS Then Reverse: intuition, complexity, and code

Hints

Hint 1
Discover one complete depth at a time with a saved queue size.
Hint 2
Alternate only the output order; enqueue left then right children on every level.
Hint 3
For a level of size k, reverse position i by writing at k - 1 - i.

Edge Cases

  • An empty tree returns [], and a singleton returns [[value]].
  • A 2000-node chain has a singleton on every level, so reversing changes nothing; iterative BFS avoids stack depth limits.
  • Missing children are skipped, not output as null; Example 4 still preserves left-to-right discovery for the third level.
  • Duplicate and negative values are separate occurrences; direction depends on depth, not on values.
  • A broad tree maximizes queue width; every output level must own a distinct mutable list.

Common Mistakes and Interview Tips

  • Reversing child enqueue order along with the output changes the next level incorrectly.
  • Reading the live queue size during the inner loop lets newly added children enter the current level.
  • Using shift() or unshift() per value in JavaScript, or Python pop(0), repeatedly copies wide arrays and can become quadratic.
  • Using k - i instead of k - 1 - i writes outside the level.
  • Flipping direction per node instead of per level produces mixed output.
  • Both approaches are O(n); direct indexing removes a pass, not an asymptotic factor. Keep results local and allocate a fresh list per level.

Key Takeaway

Level-order problems are breadth-first searches with per-level bookkeeping, so a small flag or index tweak adapts the same traversal to zigzag orders, averages, and side views.