DSA Notes
Learn how to draw and analyze recursion trees — the most intuitive method for understanding recursive algorithm complexity. Covers single and multi-branch recursion, level-by-level work calculation, geometric series sums, and how recursion trees explain Merge Sort, Fibonacci, and divide-and-conquer algorithms.
What Is a Recursion Tree?
A recursion tree is a diagram that shows every function call made by a recursive algorithm as a node in a tree. Each node represents one call, its children represent the recursive calls it makes, and the value at each node represents the non-recursive work done at that call.
Reading a recursion tree tells you:
- How many total function calls are made
- How work is distributed across recursive levels
- The time complexity (by summing all work across all nodes)
- The space complexity (by finding the maximum depth/height)
Example 1 — Merge Sort: T(n) = 2T(n/2) + O(n)
Recursion tree for n=8:
| Level 0 | merge_sort(8) Work: n = 8 |
| Level 1 | merge_sort(4) merge_sort(4) Work: n/2+n/2 = n = 8 |
| Level 2 | ms(2) ms(2) ms(2) ms(2) Work: 4×(n/4) = n = 8 |
| Level 3 | (1)(1)(1)(1)(1)(1)(1)(1) Work: 8×(1) = n = 8 |
| Each level | n total work |
| Number of levels | log₂(n) |
| Total work | n × log₂(n) = O(n log n) |
Example 2 — Naive Fibonacci: T(n) = T(n-1) + T(n-2) + O(1)
def fib(n):
if n <= 1: return n
return fib(n-1) + fib(n-2) # Two calls, decreasing by 1 and 2Recursion tree for fib(5):
| Level 0 | 1 |
| Level 1 | 2 |
| Level 2 | 4 (approximately, some branches stop at fib(0)/fib(1)) |
| Level 3 | ~8 |
| Level k | ~2^k |
This explains why naive Fibonacci is O(2ⁿ).
Example 3 — Binary Search: T(n) = T(n/2) + O(1)
| Level 0 | search(n) Work: 1 |
| Level 1 | search(n/2) Work: 1 |
| Level 2 | search(n/4) Work: 1 |
| Level k | search(n/2^k) Work: 1 |
| Level log₂(n) | search(1) BASE CASE |
| Total levels | log₂(n) |
| Work per level | 1 |
| Total work | log₂(n) × 1 = O(log n) |
Example 4 — T(n) = 3T(n/4) + O(n²)
Three subproblems, each one-quarter the size:
| Level 0 | n² Total: n² |
| Level 1 | 3 calls of (n/4)² Total: 3(n/4)² = 3n²/16 |
| Level 2 | 9 calls of (n/16)² Total: 9(n/16)² = 9n²/256 |
| Level k | 3^k calls of (n/4^k)² Total: 3^k × (n/4^k)² |
| Number of levels | log₄(n) (since input shrinks by ×4 each level) |
| The geometric series converges | first level dominates → O(n²) |
Key insight: When the geometric ratio r < 1, the ROOT level dominates, and T(n) = O(work at root) = O(f(n)).
Example 5 — T(n) = 2T(n/2) + O(1) (Counting Binary Tree Nodes)
| Level 0 | 1 call on n Work per node: 1 Total: 1 |
| Level 1 | 2 calls on n/2 Work per node: 1 Total: 2 |
| Level 2 | 4 calls on n/4 Work per node: 1 Total: 4 |
| Level k | 2^k calls on n/2^k Work per node: 1 Total: 2^k |
| Level log₂(n) | n leaf calls Total: n |
Key insight: When the geometric ratio r > 1, the LEAVES dominate. T(n) = O(number of leaves) = O(n^(log_b a)).
The Three Cases via Recursion Tree
| Ratio r = f(n) / n^(log_b a) | Dominance | T(n) |
|---|---|---|
| r < 1 (shrinking) | Root dominates | Θ(f(n)) |
| r = 1 (balanced) | All levels equal | Θ(n^(log_b a) × log n) |
| r > 1 (growing) | Leaves dominate | Θ(n^(log_b a)) |
This is exactly the Master Theorem, but now you understand *why* it works.
Reading Recursion Trees for Backtracking
For arr of length n:
| Each of n positions | 2 choices (include/exclude) |
| Tree | binary tree of depth n |
| Total nodes | 2⁰ + 2¹ + ... + 2ⁿ = 2^(n+1) - 1 = O(2ⁿ) |
| Each leaf takes O(n) to print | Total: O(n × 2ⁿ) |
Space Complexity from Recursion Tree
The space complexity equals the maximum number of stack frames active simultaneously = the maximum depth of the recursion tree.
| Algorithm | Depth | Space |
|---|---|---|
| Merge Sort | log n | O(log n) |
| Binary Search | log n | O(log n) |
| Naive Fibonacci | n | O(n) |
| Linear Recursion | n | O(n) |
| Tower of Hanoi | n | O(n) |
| Quick Sort (avg) | log n | O(log n) |
| Quick Sort (worst) | n | O(n) |
Summary
A recursion tree is the most intuitive method for analyzing recursive algorithms:
- Draw the tree with work labeled at each node
- Calculate total work per level
- Count the number of levels
- Sum across all levels (often a geometric series)
Three key patterns:
- Balanced work (Merge Sort): each level does the same total work → multiply by levels
- Decreasing work (Fast exponentiation): root dominates → root-level work is the answer
- Increasing work (Tree counting): leaves dominate → count the leaves
*Next Lesson: Recursion vs Iteration — When to use each approach and how to convert between them.*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Recursion Tree — Visualizing Recursion for Complexity Analysis.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, recursion, tree, recursion tree — visualizing recursion for complexity analysis
Related Data Structures & Algorithms Topics