101 Logo
onenoughtone

Level Order Traversal Pattern

What is Level Order Traversal?

Level Order Traversal is a BFS application that visits all nodes in a tree level by level, from left to right.

It's also known as Breadth-First Tree Traversal because it explores all nodes at the current level before moving to the next level.

Problem Statement

Given a binary tree, return the level order traversal of its nodes' values (i.e., from left to right, level by level).

Example:

    3
  /  \
 9  20
    / \
   15  7

Level order traversal: [[3], [9, 20], [15, 7]]

BFS Approach

  1. Start with the root node and enqueue it into a queue
  2. While the queue is not empty:
    • Get the number of nodes at the current level (queue size)
    • Process all nodes at the current level and enqueue their children
    • Add the current level's values to the result
  3. Return the result containing all levels

Implementation

Here's an implementation of the level order traversal algorithm:

class TreeNode { constructor(val) { this.val = val; this.left = null; this.right = null; } } function levelOrderTraversal(root) { // If the tree is empty, return an empty array if (!root) { return []; } // Result array to store the level order traversal const result = []; // Queue for BFS const queue = [root]; // While there are nodes in the queue while (queue.length > 0) { // Get the number of nodes at the current level const levelSize = queue.length; const currentLevel = []; // Process all nodes at the current level for (let i = 0; i < levelSize; i++) { // Dequeue a node from the queue const node = queue.shift(); // Add the node's value to the current level currentLevel.push(node.val); // Enqueue the node's children if (node.left) { queue.push(node.left); } if (node.right) { queue.push(node.right); } } // Add the current level to the result result.push(currentLevel); } return result; } // Example usage: const root = new TreeNode(3); root.left = new TreeNode(9); root.right = new TreeNode(20); root.right.left = new TreeNode(15); root.right.right = new TreeNode(7); const levels = levelOrderTraversal(root); console.log(levels); // Output: [[3], [9, 20], [15, 7]]

Time and Space Complexity

Time Complexity:

O(n), where n is the number of nodes in the tree. We visit each node exactly once.

Space Complexity:

O(n), where n is the number of nodes in the tree. In the worst case, the queue might contain all nodes at the last level, which can be up to n/2 nodes.

Variation: Zigzag Level Order Traversal

A common variation is the zigzag level order traversal, where we traverse the tree in a zigzag pattern (left to right, then right to left, and so on).

function zigzagLevelOrder(root) { // If the tree is empty, return an empty array if (!root) { return []; } // Result array to store the zigzag level order traversal const result = []; // Queue for BFS const queue = [root]; // Flag to indicate the direction of traversal let leftToRight = true; // While there are nodes in the queue while (queue.length > 0) { // Get the number of nodes at the current level const levelSize = queue.length; const currentLevel = []; // Process all nodes at the current level for (let i = 0; i < levelSize; i++) { // Dequeue a node from the queue const node = queue.shift(); // Add the node's value to the current level based on the direction if (leftToRight) { currentLevel.push(node.val); } else { currentLevel.unshift(node.val); } // Enqueue the node's children if (node.left) { queue.push(node.left); } if (node.right) { queue.push(node.right); } } // Add the current level to the result result.push(currentLevel); // Flip the direction for the next level leftToRight = !leftToRight; } return result; } // Example usage: const root = new TreeNode(3); root.left = new TreeNode(9); root.right = new TreeNode(20); root.right.left = new TreeNode(15); root.right.right = new TreeNode(7); const levels = zigzagLevelOrder(root); console.log(levels); // Output: [[3], [20, 9], [15, 7]]
IntroVisualizePatternsPractice
101 Logo
onenoughtone