Recursion Tree Method to Solve Recurrences (original) (raw)

Last Updated : 11 Jan, 2026

The recursion tree method is used to analyze the time complexity of recursive algorithms by visually representing the recurrence as a tree. Each node of the tree represents the work done in a single recursive call, and each level represents one stage of the recursion. Below are the steps used to find time complexity using recursion tree method.

  1. Draw a recursive tree for given recurrence relation
  2. Calculate the cost at each level and count the total no of levels in the recursion tree.
  3. Sum up the cost of all the levels in the recursive tree

**Note: If summing up all the levels becomes complex, we can find an upper bound by considering a perfectly full tree and / or an infinite geometrical series (the ratio is typically less than 1).

Let us take the below example code to understand the steps discussed above

C++ `

void fun(int n) {

if (n <= 1)
    return;

fun(n / 2);
fun(n / 2);

for (int i = 0; i < n; i++) {
    cout << "GFG ";
}

}

Java

static void fun(int n) {

if (n <= 1)
    return;

fun(n / 2);
fun(n / 2);

for (int i = 0; i < n; i++) {
    System.out.print("GFG ");
}

}

Python

def fun(n):

if n <= 1:
    return

fun(n // 2)
fun(n // 2)

for i in range(n):
    print("GFG", end=" ")

C#

static void fun(int n) {

if (n <= 1)
    return;

fun(n / 2);
fun(n / 2);

for (int i = 0; i < n; i++) {
    Console.Write("GFG ");
}

}

JavaScript

function fun(n) {

if (n <= 1)
    return;

fun(Math.floor(n / 2));
fun(Math.floor(n / 2));

for (let i = 0; i < n; i++) {
    process.stdout.write("GFG ");
}

}

`

Understanding the Recursion for This Code

The function makes two recursive calls, each on a subproblem of size n/2. After both recursive calls return, it performs a loop that prints "GFG" exactly n times, which is linear work for each function call. The recursion stops when n ≤ 1.

Writing the Recurrence Relation

Each function call makes two recursive calls of size n/2 and performs linear work after the recursion.
Therefore, the recurrence relation is: T(n) = 2T(n/2) + O(n)

To find T(n), we analyze the recursion tree and sum the cost at each level.

**Level 0: The root contributes a cost of cn.

**Level 1: The subproblems are of sizes n/2 and n/2.
Their total cost is: c(n/2)+ c(n/2) = cn

**Level 2: There are four subproblems of size n/4.
Continuing this way, the work at each level reamains cn.

Total Work Across All Levels

The input size halves at each level, and the recursion stops when the size becomes constant.
Hence, the height of the recursion tree is log⁡n.

Since each level contributes a cost of cn, the total work is:
T(n) = cn + cn + ....... + cn (log n levels)
T(n) = cn log n

**Final Result: T(n) = O(n log n)

Another Example with Unequal Subproblems

Let us see the below code as another recursive code to be analyzed using recursion tree method.

C++ `

void fun(int n) {

if (n <= 1)
    return;

fun(n / 4);
fun(n / 2);

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        cout << "GFG ";
    }
}

}

Java

static void fun(int n) {

if (n <= 1)
    return;

fun(n / 4);
fun(n / 2);

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        System.out.print("GFG ");
    }
}

}

Python

def fun(n):

if n <= 1:
    return

fun(n // 4)
fun(n // 2)

for i in range(n):
    for j in range(n):
        print("GFG", end=" ")

C#

static void fun(int n) {

if (n <= 1)
    return;

fun(n / 4);
fun(n / 2);

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        Console.Write("GFG ");
    }
}

}

JavaScript

function fun(n) {

if (n <= 1)
    return;

fun(Math.floor(n / 4));
fun(Math.floor(n / 2));

for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
        process.stdout.write("GFG ");
    }
}

}

`

Understanding the Recursion for This Code

Writing the Recurrence Relation

The recursive calls contribute T(n/4) and T(n/2). The nested loops perform Θ(n²) work. Therefore, the recurrence relation is: T(n) = T(n/4) + T(n/2) + O(n2)

Issue with Recursion Trees Having Unequal Subproblems

In recursion trees where the problem does not split into equal-sized subproblems, the height of the recursion tree is not uniform across all branches. In the given example, one recursive call reduces the problem size to n/2, while the other reduces it to n/4. As a result, some branches of the recursion tree terminate earlier, while others continue deeper.

Because of this imbalance:

When we apply the geometric progression (GP) method to sum the cost across levels, we implicitly assume that all subproblems continue until the same depth. This assumption is not strictly true for recurrences with unequal subproblem sizes.

However, to simplify the analysis and still obtain a valid asymptotic bound, we take an upper-bound approach:

To find T(n), we analyze the recursion tree and sum the cost at each level.

**Final Result: T(n) = O(n2)