DSA Tracker

Problem

Pattern this teaches:Tree BFS and Level Order

Medium

Binary Tree Zigzag Level Order Traversal

A medium Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Binary Trees
Sheets
2
Core for
4 roles

The problem

Read a binary tree level by level, but switch direction on every level: the first level left to right, the next right to left, and so on. Return one list of values per level.

Example 1

Input
[5,3,7,1,4,6,8]
Output
[[5],[7,3],[1,4,6,8]]
Why
Level 0 LTR: 5. Level 1 RTL: 7,3. Level 2 LTR: 1,4,6,8.

Example 2

Input
[1]
Output
[[1]]
Why
A single node forms one level.

Example 3

Input
[1,2,3,4,null,null,5]
Output
[[1],[3,2],[4,5]]
Why
Level 0: [1], level 1 reversed: [3,2], level 2: [4,5].

Constraints

  • The number of nodes is in the range [0, 2000].
  • -1000 <= Node.val <= 1000

How to think about it

Updated 2026-09-29

The underlying traversal of the tree must stay strictly left-to-right so parent relationships are never jumbled. The zigzag alternation is entirely a presentation choice: write elements into the level buffer either forwards or backwards, toggling a direction flag once each level finishes.

Approaches, worst first

  1. Standard BFS with level reversal

    time O(n) · space O(n)

    Perform standard level order traversal. On every odd-indexed level, reverse the collected array before pushing it to the final result. Extra work is done reversing arrays after collection.

  2. BFS with direct index placementWrite this one

    time O(n) · space O(n)

    Measure level size `k`. Allocate a fixed-size row of length `k`. If moving left to right, place values at index `i`; if right to left, place at `k - 1 - i`. Eliminates reversing steps completely while keeping FIFO child enqueuing intact.

Where people lose marks · 3
  • Reversing the order in which child nodes are enqueued into the BFS queue corrupts the structural discovery order for all future descendant levels.
  • Forgetting to toggle the direction boolean flag between successive levels causes all levels beyond the first alternation to face the wrong direction.
  • Null root returning `[[]]` instead of an empty array `[]`.

The theory behind it

Binary Trees — the ground this problem stands on. All Binary Trees problems

What Binary Trees is

A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.

When to reach for it

Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.

How the pattern works

Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.

More Binary Trees problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .