How to solve time complexity Recurrence Relations using Recursion Tree method?
Last Updated :
05 Jul, 2024
The Recursion Tree Method is a way of solving recurrence relations. In this method, a recurrence relation is converted into recursive trees. Each node represents the cost incurred at various levels of recursion. To find the total cost, costs of all levels are summed up.
Steps to solve recurrence relation using recursion tree method:
- Draw a recursive tree for given recurrence relation
- Calculate the cost at each level and count the total no of levels in the recursion tree.
- Count the total number of nodes in the last level and calculate the cost of the last level
- 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 see how to solve these recurrence relations with the help of some examples:
Question 1:
T(n) = 2T(n/2) + c
Solution:Â
- Step 1: Draw a recursive treeÂ

Recursion Tree
- Step 2: Calculate the work done or cost at each level and count total no of levels in recursion treeÂ

Recursive Tree with each level cost
Count the total number of levels –Â
Choose the longest path from root node to leaf node
 n/20 -â n/21 -â n/22 -â ……… -â n/2k
Size of problem at last level = n/2k
 At last level size of problem becomes 1
 n/2k = 1
 2k = n
 k = log2(n)  Â
Total no of levels  in recursive tree = k +1 = log2(n) + 1
- Step 3: Count total number of nodes in the last level and calculate cost of last level
 No. of nodes at level 0 = 20 = 1
  No. of nodes at level 1 = 21 = 2
 ………………………………………………………
No. of nodes at the (k+1)th level (or last level) = 2k = 2log2(n) = nlog2(2) = n
 Cost of sub problems at level last level = n x Î(1) = Î(n)
- Step 4: Sum up the cost all the levels in recursive tree Â
  T(n) = c + 2c + 4c + —- ((no. of levels-1) times) + last level cost
 = c + 2c + 4c + —- (log2(n) times) + Î(n)
 = c(1 + 2 + 4 + —- (log2(n) times)) + Î(n)
 1 + 2 + 4 + —– (log2(n) times) –> 20 + 21 + 22 + —– (log2(n) times) –> Geometric Progression(G.P.)
Using GP Sum Formula, we get the sum of the series as n
= c(n) + Î(n)
Thus, T(n) = Î(n)
Question 2: T(n) = T(n/10) + T(9n/10) + n
Solution:Â
- Step 1: Draw a recursive tree
Â

Recursive Tree
- Step 2: Calculate the work done or cost at each level and count total no of levels in recursion tree

Recursion Tree with each level cost
 Count the total number of levels –
 Choose the longest path from root node to leaf node
(9/10)0n –> (9/10)1n –> (9/10)2n –> ……… –> (9/10)kn
 Size of problem at last level = (9/10)kn
 At last level size of problem becomes 1
(9/10)kn = 1
n = (10/9)k
  k = log10/9(n)
 Total no of levels in recursive tree = k +1 = log10/9(n) + 1
- Step 3: Count total number of nodes in the last level and calculate cost of last level
Please note that it will be skewed tree as we have left child having n/10 value and right child having 9n/10 value (division factor for the left child is much higher). So the last level would have constant number of nodes or Î(1) and there work done at the last level is Î(1) * Î(1) = Î(1)
- Step 4: Sum up the cost all the levels in recursive tree. Please note that we consider a perfectly full tree till the second last level here for simplicity and calculate an upper bound.
  T(n) < cn + cn + cn + —- ((no. of levels-1) times) + last level cost
 < cn(1 + 1 + 1 + —- (log10/9(n) times)) + Î(1)
 < cn log10/9(n) + Î(1)
Thus, T(n) = O(nlog10/9(n)) + Î(1) = O(nlog10/9(n))
Similar Reads
How to solve time complexity Recurrence Relations using Recursion Tree method?
The Recursion Tree Method is a way of solving recurrence relations. In this method, a recurrence relation is converted into recursive trees. Each node represents the cost incurred at various levels of recursion. To find the total cost, costs of all levels are summed up. Steps to solve recurrence rel
4 min read
Substitution Method to solve Recurrence Relations
Recursion is a method to solve a problem where the solution depends on solutions to smaller subproblems of the same problem. Recursive functions (function calling itself) are used to solve problems based on Recursion. The main challenge with recursion is to find the time complexity of the Recursive
6 min read
How to analyse Complexity of Recurrence Relation
The analysis of the complexity of a recurrence relation involves finding the asymptotic upper bound on the running time of a recursive algorithm. This is usually done by finding a closed-form expression for the number of operations performed by the algorithm as a function of the input size, and then
7 min read
Print Root-to-Leaf Paths in a Binary Tree Using Recursion
Given a Binary Tree of nodes, the task is to find all the possible paths from the root node to all the leaf nodes of the binary tree. Example: Input: Output: 1 2 41 2 51 3 Approach: The approach involves using recursion to traverse a Binary tree. The recursive function will append node values to a p
6 min read
Different types of recurrence relations and their solutions
In this article, we will see how we can solve different types of recurrence relations using different approaches. Before understanding this article, you should have idea about recurrence relations and different method to solve them (See : Worst, Average and Best Cases, Asymptotic Notations, Analysis
3 min read
Solving Homogeneous Recurrence Equations Using Polynomial Reduction
A recurrence relation is an equation that recursively defines a sequence or multidimensional array of values, once one or more initial terms are given; each further term of the sequence or array is defined as a function of the preceding terms. Below are the steps required to solve a recurrence equat
8 min read
Find geometric sum of the series using recursion
Given an integer N, we need to find the geometric sum of the following series using recursion. 1 + 1/3 + 1/9 + 1/27 + ... + 1/(3^n) Examples: Input N = 5 Output: 1.49794 Input: N = 7 Output: 1.49977 Approach:In the above-mentioned problem, we are asked to use recursion. We will calculate the last te
3 min read
Recurrence Relations | A Complete Guide
Have you ever wondered how to calculate the time complexity of algorithms like Fibonacci Series, Merge Sort, etc. where the problem is solved by dividing it into subproblems. This is done by analyzing the Recurrence Relations of these algorithms. In this article, we will learn about the basics of Re
9 min read
Product of nodes at k-th level in a tree represented as string using Recursion
Prerequisite: Product of nodes at k-th level in a tree represented as stringGiven an integer âKâ and a binary tree in string format. Every node of a tree has value in range from 0 to 9. We need to find product of elements at K-th level from the root. The root is at level 0.Note: Tree is given in the
5 min read
Print Fibonacci Series in reverse order using Recursion
Given an integer N, the task is to print the first N terms of the Fibonacci series in reverse order using Recursion. Examples: Input: N = 5Output: 3 2 1 1 0Explanation: First five terms are - 0 1 1 2 3. Input: N = 10Output: 34 21 13 8 5 3 2 1 1 0 Approach: The idea is to use recursion in a way that
3 min read