# 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 relation using recursion tree method:**

- Draw a recursive tree for given recurrence relation
- Calculate
- 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

**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

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

**Count the total number of levels –**

Choose the longest path from root node to leaf node

n/2

^{0}-→ n/2^{1}-→ n/2^{2}-→ ……… -→ n/2^{k}

Size of problem at last level = n/2^{k}

^{ }At last level size of problem becomes 1

n/2

^{k}= 12

^{k}= n

k = log_{2}(n)

**Total no of levels in recursive tree = k +1 = log _{2}(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 = 2

^{0}= 1

^{ }No. of nodes at level 1 = 2^{1}= 2………………………………………………………

No. of nodes at level log

_{2}(n) = 2^{log}_{2}^{(n)}= n^{log}_{2}^{(2) }= nCost of sub problems at level log

_{2}(n) (last level) = nxT(1) = nx1 = 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 + —- + log

_{2}(n) times + Θ(n)= c(1 + 2 + 4 + —- + log

_{2}(n) times) + Θ(n)1 + 2 + 4 + —– + log

_{2}(n) times –> 2^{0}+ 2^{1}+ 2^{2}+ —– + log2(n) times –> Geometric Progression(G.P.)= 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

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

**Count the total number of levels –**

Choose the longest path from root node to leaf node

(9/10)

^{0}n –> (9/10)^{1}n –> (9/10)^{2}n –> ……… –> (9/10)^{k}nSize of problem at last level = (9/10)

^{k}nAt last level size of problem becomes 1

(9/10)

^{k}n = 1(9/10)

^{k}= 1/n

k = log_{10/9}(n)

**Total no of levels in recursive tree = k +1 = log _{10/9}(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 = 2

^{0}= 1No. of nodes at level 1 = 2

^{1}= 2………………………………………………………

No. of nodes at level log

_{10/9}(n) = 2^{log}_{10/9}^{(n)}= n^{log}_{10/9}^{(2)}Cost of sub problems at level log2(n) (last level) = n

^{log}_{10/9}^{(2)}x T(1) = n^{log}_{10/9}^{(2)}x 1 = n^{log}_{10/9}^{(2)}

Step 4:Sum up the cost all the levels in recursive treeT(n) = n + n + n + —- + (no. of levels – 1) times + last level cost

= n + n + n + —- + log

_{10/9}(n) times + Θ(n^{log}_{10/9}^{(2)})= nlog

_{10/9}(n) + Θ(n^{log}_{10/9}^{(2)})

Thus, __T(n) = Θ(nlog___{10/9}__(n))__^{ }