The Recursion-tree Method
Updated Jun 30, 20253 min read
The Recursion Tree Method is a visual technique used to solve recurrence relations, which arise in the analysis of recursive algorithms. It helps in determining the time complexity of recursive algorithms by breaking down the problem into smaller subproblems and visualizing the cost at each level of recursion.
Key Concepts of Recursion Tree Method
- Recurrence Relation:
- A recurrence relation expresses the running time of a recursive algorithm in terms of the size of the input (n) and the time taken by its subproblems.
- Example: T(n) = aT(n/b) + f(n)
a: Number of subproblems.n/b: Size of each subproblem.f(n): Cost of dividing the problem and combining results.
- Purpose of Recursion Tree:
- To visualize how the recurrence relation expands.
- To calculate the total cost of the algorithm by summing up the costs at all levels of recursion.
- Steps to Solve Using Recursion Tree:
- Step 1: Expand the recurrence relation into a tree structure.
- Step 2: Calculate the cost at each level of the tree.
- Step 3: Sum up the costs of all levels to find the total cost.
- Step 4: Use asymptotic notation (Big O) to express the time complexity.
How to Use the Recursion Tree Method
Example 1: Solve T(n) = 2T(n/2) + n
Expand the Recurrence:
- At the root (Level 0), the cost is
n. - At Level 1, the recurrence splits into 2 subproblems, each of size
n/2. The cost at this level is2 × (n/2) = n. - At Level 2, each subproblem splits again into 2 subproblems of size
n/4. The cost is4 × (n/4) = n. - This process continues until the subproblem size becomes 1.
- At the root (Level 0), the cost is
Visualize the Tree:

Calculate the Total Cost:
- Cost at each level =
n. - Number of levels = log₂n (since the size of the subproblem reduces by half at each level).
- Total cost = Cost per level × Number of levels =
n × log₂n.
- Cost at each level =
Final Complexity:
- T(n) = O(n log n).
Example 2: Solve T(n) = T(n/2) + n
Expand the Recurrence:
- At Level 0, the cost is
n. - At Level 1, the cost is
n/2. - At Level 2, the cost is
n/4. - This continues until the subproblem size becomes 1.
Visualize the Tree:

Calculate the Total Cost:
- The costs form a geometric series:
n + n/2 + n/4 + ... + 1. - Sum of the series = 2n (approximation for infinite geometric series).
Final Complexity:
T(n) = O(n).
Balanced Recursion Tree:
- If the recursion divides the problem into multiple subproblems of equal size (e.g., T(n) = 2T(n/2)), the tree is balanced.
- The total cost is often proportional to the number of levels multiplied by the cost at each level.
Unbalanced Recursion Tree:
- If the recursion divides the problem into one smaller subproblem (e.g., T(n) = T(n/2)), the tree is unbalanced.
- The total cost is dominated by the cost at the root level.
Advantages of Recursion Tree Method
- Visualization: Provides a clear picture of how the recurrence expands.
- Intuitive: Easy to understand and apply for simple recurrence relations.
- Useful for Divide-and-Conquer Algorithms: Helps analyze algorithms like Merge Sort, Quick Sort, and Binary Search.
Limitations
- Not suitable for complex recurrence relations.
- Requires additional techniques (like Master Theorem) for precise analysis in some cases.
Common Applications
- Merge Sort: T(n) = 2T(n/2) + O(n) → O(n log n)
- Binary Search: T(n) = T(n/2) + O(1) → O(log n)
- Matrix Multiplication (Strassen’s Algorithm): T(n) = 7T(n/2) + O(n²) → O(n^2.81)
