Tree Traversals
Tree Traversal Algorithms
Section titled “Tree Traversal Algorithms”There are two fundamental ways to traverse a tree: DFS (goes deep first) and BFS (goes wide first).
Depth First Search (DFS)
Section titled “Depth First Search (DFS)”DFS explores a branch fully before backtracking. There are three DFS orders:
flowchart TD R[4] --> L[2] R --> Ri[6] L --> LL[1] L --> LR[3] Ri --> RL[5] Ri --> RR[7]
style R fill:#7c3aed,color:#fff style L fill:#4f46e5,color:#fff style Ri fill:#4f46e5,color:#fff style LL fill:#6366f1,color:#fff style LR fill:#6366f1,color:#fff style RL fill:#6366f1,color:#fff style RR fill:#6366f1,color:#fffInorder Traversal (Left → Root → Right)
Section titled “Inorder Traversal (Left → Root → Right)”Tree: [4] / \ [2] [6] / \ / \ [1] [3][5] [7]
Step-by-step:1. Go left all the way: 12. Visit root: 23. Go right: 34. Back up, visit: 45. Go left of right: 56. Visit: 67. Go right: 7
Inorder Output: 1 → 2 → 3 → 4 → 5 → 6 → 7 (SORTED for BST!)function inorder(root, result = []) { if (root === null) return result; inorder(root.left, result); result.push(root.val); inorder(root.right, result); return result;}Use case: Get sorted order from BST
Preorder Traversal (Root → Left → Right)
Section titled “Preorder Traversal (Root → Left → Right)”Step-by-step:1. Visit root: 42. Go left, visit: 23. Go left of 2, visit: 14. Backtrack, go right of 2, visit: 35. Backtrack to root, go right, visit: 66. Go left of 6, visit: 57. Go right of 6, visit: 7
Preorder Output: 4 → 2 → 1 → 3 → 6 → 5 → 7function preorder(root, result = []) { if (root === null) return result; result.push(root.val); preorder(root.left, result); preorder(root.right, result); return result;}Use case: Copying/serializing a tree, Expression trees (prefix notation)
Postorder Traversal (Left → Right → Root)
Section titled “Postorder Traversal (Left → Right → Root)”Step-by-step:1. Go left all the way, leaf: 12. Go right of 2, leaf: 33. Visit parent of 1 and 3: 24. Go right subtree, left: 55. Right: 76. Visit: 67. Visit root last: 4
Postorder Output: 1 → 3 → 2 → 5 → 7 → 6 → 4function postorder(root, result = []) { if (root === null) return result; postorder(root.left, result); postorder(root.right, result); result.push(root.val); return result;}Use case: Deleting a tree, Evaluating expression trees, Directory size calculation
Iterative Inorder (Using Explicit Stack)
Section titled “Iterative Inorder (Using Explicit Stack)”function inorderIterative(root) { const result = []; const stack = []; let curr = root;
while (curr !== null || stack.length > 0) { while (curr !== null) { // Go as left as possible stack.push(curr); curr = curr.left; } curr = stack.pop(); // Visit node result.push(curr.val); curr = curr.right; // Move to right subtree } return result;}Breadth First Search (Level Order Traversal)
Section titled “Breadth First Search (Level Order Traversal)”Visit all nodes level by level, from left to right. Uses a Queue.
Tree: [1] / \ [2] [3] / \ \ [4] [5] [6]
Step-by-step:Queue: [1]→ Dequeue 1, enqueue children [2, 3] → Output: 1→ Dequeue 2, enqueue children [4, 5] → Output: 2→ Dequeue 3, enqueue children [6] → Output: 3→ Dequeue 4, no children → Output: 4→ Dequeue 5, no children → Output: 5→ Dequeue 6, no children → Output: 6
Level Order Output: 1 → 2 → 3 → 4 → 5 → 6function levelOrder(root) { if (!root) return [];
const result = []; const queue = [root];
while (queue.length > 0) { const levelSize = queue.length; // Number of nodes at current level const currentLevel = [];
for (let i = 0; i < levelSize; i++) { const node = queue.shift(); currentLevel.push(node.val);
if (node.left) queue.push(node.left); if (node.right) queue.push(node.right); } result.push(currentLevel); } return result;}
// Example Output for [1, 2, 3, 4, 5]:// [ [1], [2, 3], [4, 5] ]Key trick: Capture
queue.lengthat the start of each iteration to process one full level at a time.
Quick Recap
Section titled “Quick Recap”┌────────────────────────────────────────────────────────┐│ TRAVERSAL QUICK RECALL │├────────────────────────────────────────────────────────┤│ Inorder (L → N → R): Sorted output for BST ││ Preorder (N → L → R): Root first; use for copy/serialize ││ Postorder (L → R → N): Children first; use for delete ││ Level Order: BFS; use for level problems │└────────────────────────────────────────────────────────┘Related
Section titled “Related”- Important Patterns — DFS recursion, BFS level-order patterns
- BST Operations — Search, Insert, Delete
- Code Examples — Full implementations