0
0
DSA Typescriptprogramming

Zigzag Level Order Traversal in DSA Typescript

Choose your learning style9 modes available
Mental Model
We visit nodes level by level, but switch direction each level, going left to right then right to left alternately.
Analogy: Imagine reading lines of text where the first line you read left to right, the next line right to left, then left to right again, like zigzagging across the page.
      1
     / \
    2   3
   / \   \
  4   5   6

Level 0: 1 (left to right)
Level 1: 3 -> 2 (right to left)
Level 2: 4 -> 5 -> 6 (left to right)
Dry Run Walkthrough
Input: Binary tree: 1 as root, 2 and 3 as children of 1, 4 and 5 as children of 2, 6 as right child of 3
Goal: Traverse the tree level by level, but alternate direction each level to get zigzag order
Step 1: Start at root level (level 0), direction left to right
Level 0: [1]
Output: [[1]]
Why: We begin traversal from the root, going left to right
Step 2: Move to level 1, switch direction to right to left
Level 1 nodes: 2, 3
Output: [[1], [3, 2]]
Why: We reverse order at this level to get zigzag pattern
Step 3: Move to level 2, switch direction back to left to right
Level 2 nodes: 4, 5, 6
Output: [[1], [3, 2], [4, 5, 6]]
Why: Direction alternates again to left to right
Result:
Final output: [[1], [3, 2], [4, 5, 6]]
Annotated Code
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function zigzagLevelOrder(root: TreeNode | null): number[][] {
  if (!root) return [];
  const result: number[][] = [];
  const queue: TreeNode[] = [root];
  let leftToRight = true;

  while (queue.length > 0) {
    const levelSize = queue.length;
    const levelNodes: number[] = [];

    for (let i = 0; i < levelSize; i++) {
      const node = queue.shift()!; // remove front node
      // Add node value based on current direction
      if (leftToRight) {
        levelNodes.push(node.val); // left to right
      } else {
        levelNodes.unshift(node.val); // right to left
      }
      // Add children for next level
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
    result.push(levelNodes);
    leftToRight = !leftToRight; // flip direction
  }
  return result;
}

// Driver code to test
const root = new TreeNode(1,
  new TreeNode(2, new TreeNode(4), new TreeNode(5)),
  new TreeNode(3, null, new TreeNode(6))
);
const output = zigzagLevelOrder(root);
console.log(JSON.stringify(output));
if (!root) return [];
handle empty tree edge case
const queue: TreeNode[] = [root];
initialize queue with root for level order traversal
while (queue.length > 0) {
process nodes level by level until none left
const levelSize = queue.length;
determine number of nodes at current level
const node = queue.shift()!;
remove front node to process current level
if (leftToRight) { levelNodes.push(node.val); } else { levelNodes.unshift(node.val); }
add node value in correct order based on direction
if (node.left) queue.push(node.left); if (node.right) queue.push(node.right);
enqueue children for next level
leftToRight = !leftToRight;
flip direction for next level
OutputSuccess
[[1],[3,2],[4,5,6]]
Complexity Analysis
Time: O(n) because each node is visited exactly once during traversal
Space: O(n) because the queue and result store nodes and values for each level
vs Alternative: Compared to normal level order traversal, zigzag adds a small overhead for reversing order but still remains O(n)
Edge Cases
empty tree (root is null)
returns empty array immediately
DSA Typescript
if (!root) return [];
tree with single node
returns array with one level containing that node
DSA Typescript
const queue: TreeNode[] = [root];
tree with only left or only right children
still traverses levels correctly alternating direction
DSA Typescript
if (node.left) queue.push(node.left); if (node.right) queue.push(node.right);
When to Use This Pattern
When a problem asks for level order traversal but with alternating directions each level, use zigzag level order traversal to handle the direction switch cleanly.
Common Mistakes
Mistake: Always appending node values left to right without reversing on alternate levels
Fix: Use unshift to add values at front on alternate levels to reverse order
Mistake: Reversing the entire level array after collecting all nodes instead of inserting in correct order
Fix: Insert nodes in correct order during traversal to avoid extra reversal step
Summary
Traverses a binary tree level by level, alternating direction each level to create a zigzag pattern.
Use when you need to visit nodes in a breadth-first manner but want to alternate the order of nodes at each level.
The key insight is to switch the order of inserting node values at each level instead of reversing after traversal.