Skip to content
DSA Patterns
Menu
Language
Tree BFSLC #1032 of 6← PreviousNext →

Demo preview: These solutions pass the automated test suite but have not been human-reviewed. Treat trade-offs and prose as draft.

Mediumtree-bfs

Binary Tree Zigzag Level Order Traversal

Problem (restated)

Level-order values, but alternate left→right and right→left each level.

Intuition

BFS levels; reverse odd levels (or use deque direction).

Approaches

Level BFS + reverse

Unverified
Time O(n)Space O(w)

Idea. collect level as usual; if depth odd reverse before push.

Walkthrough. [3,9,20,null,null,15,7] → [[3],[20,9],[15,7]].

Trade-offs. Deque insert-front is optional sugar; reverse is clear.

Solution
Show helper code
export class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val = 0, left: TreeNode | null = null, right: TreeNode | null = null) {
    this.val = val; this.left = left; this.right = right;
  }
}
export class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val = 0, left: TreeNode | null = null, right: TreeNode | null = null) {
    this.val = val; this.left = left; this.right = right;
  }
}
export function zigzagLevelOrder(root: TreeNode | null): number[][] {
  if (!root) return [];
  const res: number[][] = [];
  let level: TreeNode[] = [root];
  let leftToRight = true;
  while (level.length) {
    const next: TreeNode[] = [];
    const vals: number[] = [];
    for (const n of level) {
      vals.push(n.val);
      if (n.left) next.push(n.left);
      if (n.right) next.push(n.right);
    }
    if (!leftToRight) vals.reverse();
    res.push(vals);
    level = next;
    leftToRight = !leftToRight;
  }
  return res;
}
export function zigzagLevelOrder(root: TreeNode | null): number[][] {
  if (!root) return [];
  const res: number[][] = [];
  let level: TreeNode[] = [root];
  let leftToRight = true;
  while (level.length) {
    const next: TreeNode[] = [];
    const vals: number[] = [];
    for (const n of level) {
      vals.push(n.val);
      if (n.left) next.push(n.left);
      if (n.right) next.push(n.right);
    }
    if (!leftToRight) vals.reverse();
    res.push(vals);
    level = next;
    leftToRight = !leftToRight;
  }
  return res;
}

Template connection

The same level snapshot. Odd depths reverse the collected values before recording them. Even depths stay left to right.

Reflection

  • Level BFS stays the same. Reverse each odd level’s list to get the zigzag.
  • Always enqueue left to right. The reverse applies only to that level’s output.
  • One node shows no reverse. A skewed tree has one node on every row.
Contributor review checklist

Interactive

Mental model

A worked animation for this problem. Scrub steps or press space to pause; re-tell the invariant out loud.

Step 1 of 6
1/6

Zigzag level order on 3 / 9, 20 / 15, 7. Queue still walks left→right; reverse odd levels in the output only.

3q920157

queue = [3] · level 0 L→R

Key:CurrentActive rangeMatchSettled