Breadth-First Search
Medium

Zigzag Level Order Traversal

Traverse levels in alternating order (left-to-right, then right-to-left).
Problem Understanding

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

  • Level 0: Left -> Right
  • Level 1: Right -> Left
  • Level 2: Left -> Right
  • ...
Algorithm Strategy

We use standard BFS with a Queue to process levels one by one.

Handling Zigzag:

  1. Collect all values for the current level into a list.
  2. Check Depth: If the current level depth is Odd, reverse the list before adding it to results.
  3. Alternative: Use a Deque for the current level list, using push_front or push_back depending on depth.
Interactive Visualization
Step 1 / 1
Empty Heap

Initializing...

1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.
Unlock VisualizerPREMIUM FEATURE

Stop Guessing, Start Mastering.

Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.

Start Your Premium Prep