When you write a recursive algorithm, the total work it does is just the sum of work done at every recursive call. A recursion tree is a way to draw all those calls and add up their costs level by level . Instead of guessing the answer to a recurrence, you literally see where the work piles up.
WHY it works: A recurrence like T ( n ) = a T ( n / b ) + f ( n ) T(n) = a\,T(n/b) + f(n) T ( n ) = a T ( n / b ) + f ( n ) is self-referential. To unfold it, you keep substituting T T T into itself. The tree is a bookkeeping device for that substitution — each node is one subproblem, each node's label is the non-recursive cost f f f at that subproblem size.
Definition Recursion tree
A recursion tree is a tree where:
the root is the cost f ( n ) f(n) f ( n ) of the top-level call (excluding recursive calls),
each node of size m m m has ==a a a children== (the branching factor), each of size = = m / b = = ==m/b== == m / b == (the subproblem size),
the value written in a node is the non-recursive work f ( m ) f(m) f ( m ) done at that call.
Total runtime T ( n ) = T(n) = T ( n ) = sum of all node values over the whole tree .
The three questions you must answer for any tree:
HOW DEEP is it? (number of levels) — controlled by how fast n n n shrinks.
HOW WIDE is each level? (number of nodes at depth i i i ) — controlled by branching a a a .
HOW MUCH work per level? (sum of node values at depth i i i ).
Then T ( n ) = ∑ levels i ( work at level i ) T(n) = \sum_{\text{levels } i} (\text{work at level } i) T ( n ) = ∑ levels i ( work at level i ) .
Take the canonical divide-and-conquer recurrence:
T ( n ) = a T ( n / b ) + f ( n ) , a ≥ 1 , b > 1. T(n) = a\,T(n/b) + f(n), \quad a\ge 1,\; b>1. T ( n ) = a T ( n / b ) + f ( n ) , a ≥ 1 , b > 1.
Intuition The "three cases" come for free
Look at the level sums W ( i ) W(i) W ( i ) :
If they shrink going down → root dominates → T ( n ) = Θ ( f ( n ) ) T(n)=\Theta(f(n)) T ( n ) = Θ ( f ( n )) .
If they're roughly equal at every level → multiply one level's work by number of levels → extra log \log log factor.
If they grow going down → leaves dominate → T ( n ) = Θ ( n log b a ) T(n)=\Theta(n^{\log_b a}) T ( n ) = Θ ( n l o g b a ) .
This is the Master Theorem, but derived , not memorized.
Here a = 2 , b = 2 , f ( n ) = n a=2,\ b=2,\ f(n)=n a = 2 , b = 2 , f ( n ) = n .
Step
What we do
Why this step?
Depth
log 2 n \log_2 n log 2 n levels
Size halves each level: n / 2 i = 1 ⇒ i = log 2 n n/2^i = 1 \Rightarrow i=\log_2 n n / 2 i = 1 ⇒ i = log 2 n .
Nodes at level i i i
2 i 2^i 2 i
Branching factor a = 2 a=2 a = 2 .
Node size at level i i i
n / 2 i n/2^i n / 2 i
Size shrinks by b = 2 b=2 b = 2 .
Work per level
2 i ⋅ n 2 i = n 2^i \cdot \frac{n}{2^i} = n 2 i ⋅ 2 i n = n
The 2 i 2^i 2 i cancels — every level costs n n n .
Total
n × ( log 2 n + 1 ) = Θ ( n log n ) n \times (\log_2 n + 1) = \Theta(n\log n) n × ( log 2 n + 1 ) = Θ ( n log n )
Equal work per level × number of levels.
Worked example Read the tree
Level 0: one node, cost n n n . Level 1: two nodes, each n / 2 n/2 n /2 → total n n n . Level 2: four nodes, each n / 4 n/4 n /4 → total n n n . The work is balanced , so you simply count levels.
Here a = 3 , b = 4 , f ( n ) = n 2 a=3,\ b=4,\ f(n)=n^2 a = 3 , b = 4 , f ( n ) = n 2 .
Step
Result
Why this step?
Work at level i i i
3 i ( n 4 i ) 2 = n 2 ( 3 16 ) i 3^i\left(\frac{n}{4^i}\right)^2 = n^2\left(\frac{3}{16}\right)^i 3 i ( 4 i n ) 2 = n 2 ( 16 3 ) i
Plug into a i f ( n / b i ) a^i f(n/b^i) a i f ( n / b i ) ; group powers of i i i .
Ratio
3 16 < 1 \frac{3}{16} < 1 16 3 < 1
Geometric series with ratio < 1 <1 < 1 → decreasing .
Sum
n 2 ∑ i ( 3 / 16 ) i ≤ n 2 ⋅ 1 1 − 3 / 16 = Θ ( n 2 ) n^2\sum_i (3/16)^i \le n^2 \cdot \frac{1}{1-3/16} = \Theta(n^2) n 2 ∑ i ( 3/16 ) i ≤ n 2 ⋅ 1 − 3/16 1 = Θ ( n 2 )
Convergent geometric series → constant factor.
Total
Θ ( n 2 ) \Theta(n^2) Θ ( n 2 )
Root dominates — top level work wins.
Intuition Why root dominates
When per-level work forms a decreasing geometric series , the whole infinite sum is at most a constant times the first term. So the root's f ( n ) = n 2 f(n)=n^2 f ( n ) = n 2 sets the answer.
Subproblems shrink at different rates , so we can't use b b b directly. We bound the height .
Step
Result
Why this step?
Work per level
≤ n \le n ≤ n (sizes at a level sum to ≤ n \le n ≤ n )
n / 3 + 2 n / 3 = n n/3 + 2n/3 = n n /3 + 2 n /3 = n ; cost f f f is linear, so each level sums to about n n n .
Shortest path
log 3 n \log_3 n log 3 n (divide by 3)
The 1 / 3 1/3 1/3 branch shrinks fastest.
Longest path
log 3 / 2 n \log_{3/2} n log 3/2 n (multiply by 2 / 3 2/3 2/3 )
The 2 / 3 2/3 2/3 branch shrinks slowest → deepest.
Total
between n log 3 n n\log_3 n n log 3 n and n log 3 / 2 n n\log_{3/2} n n log 3/2 n → Θ ( n log n ) \Theta(n\log n) Θ ( n log n )
Levels × \times × per-level work n n n , all logs differ by constants.
For lopsided trees, use the recursion tree to find the height range and a per-level upper bound, then combine. The longest branch sets the depth.
Common mistake Steel-manning the classic blunders
Mistake A: "Each level costs f ( n ) f(n) f ( n ) , so total = f ( n ) ⋅ log n f(n)\cdot \log n f ( n ) ⋅ log n ."
Why it feels right: In merge sort every level genuinely costs n n n , so people overgeneralize. Fix: Level cost is a i f ( n / b i ) a^i f(n/b^i) a i f ( n / b i ) , not f ( n ) f(n) f ( n ) . It only stays constant when the a i a^i a i exactly cancels the shrink in f f f . Always compute the per-level sum.
Mistake B: "Number of leaves is n n n ."
Why it feels right: For merge sort the leaves are indeed n n n (since n log 2 2 = n n^{\log_2 2}=n n l o g 2 2 = n ). Fix: Leaves = a log b n = n log b a = a^{\log_b n} = n^{\log_b a} = a l o g b n = n l o g b a . For a = 3 , b = 2 a=3,b=2 a = 3 , b = 2 that's n 1.58 n^{1.58} n 1.58 , not n n n .
Mistake C: Forgetting the geometric ratio direction.
Why it feels right: People sum "log n \log n log n levels of something" without checking growth. Fix: Compute ratio r = a / b d r = a/b^{d} r = a / b d (where f = n d f=n^d f = n d ). r < 1 r<1 r < 1 →root, r = 1 r=1 r = 1 →log \log log factor, r > 1 r>1 r > 1 →leaves.
Recall The 20% that solves 80% of problems
For T ( n ) = a T ( n / b ) + Θ ( n d ) T(n)=aT(n/b)+\Theta(n^d) T ( n ) = a T ( n / b ) + Θ ( n d ) , compare a a a vs b d b^d b d :
a < b d a < b^d a < b d → Θ ( n d ) \Theta(n^d) Θ ( n d ) (root wins)
a = b d a = b^d a = b d → Θ ( n d log n ) \Theta(n^d \log n) Θ ( n d log n ) (balanced)
a > b d a > b^d a > b d → Θ ( n log b a ) \Theta(n^{\log_b a}) Θ ( n l o g b a ) (leaves win)
Derive it each time from the level sum n d ( a / b d ) i n^d (a/b^d)^i n d ( a / b d ) i .
Recall Feynman: explain to a 12-year-old
Imagine a boss with a big job. He splits it into smaller jobs and hands them to helpers. Each helper splits their job again, and so on, until jobs are tiny. Draw this as a family tree of jobs. To find how much total effort the whole company spends, write the effort next to each person and add up everyone — row by row is easiest. Sometimes the boss does the most work (top of the tree), sometimes the tiny workers at the bottom do (because there are SO many of them), and sometimes everyone does an equal share. The recursion tree just helps you see who's doing the heavy lifting.
Mnemonic Remember the three knobs
"DWW" — D epth = log b n =\log_b n = log b n , W idth = a i =a^i = a i nodes, W ork = a i f ( n / b i ) =a^i f(n/b^i) = a i f ( n / b i ) .
And the verdict: "Root, Repeat, Riot" — Root dominates / Repeat equally / Riot of leaves.
What three quantities must you find for any recursion tree? Depth (
log b n \log_b n log b n ), nodes per level (
a i a^i a i ), and work per level (
a i f ( n / b i ) a^i f(n/b^i) a i f ( n / b i ) ).
Total-work formula from a recursion tree for T ( n ) = a T ( n / b ) + f ( n ) T(n)=aT(n/b)+f(n) T ( n ) = a T ( n / b ) + f ( n ) ? T ( n ) = ∑ i = 0 log b n a i f ( n / b i ) T(n)=\sum_{i=0}^{\log_b n} a^i\, f(n/b^i) T ( n ) = ∑ i = 0 l o g b n a i f ( n / b i ) .
How many leaves does the tree have? a log b n = n log b a a^{\log_b n} = n^{\log_b a} a l o g b n = n l o g b a , each costing
Θ ( 1 ) \Theta(1) Θ ( 1 ) .
Per-level work for merge sort 2 T ( n / 2 ) + n 2T(n/2)+n 2 T ( n /2 ) + n , and why constant? 2 i ⋅ ( n / 2 i ) = n 2^i \cdot (n/2^i)=n 2 i ⋅ ( n / 2 i ) = n ; the branching
2 i 2^i 2 i cancels the shrink, so every level costs
n n n → total
Θ ( n log n ) \Theta(n\log n) Θ ( n log n ) .
For T ( n ) = 3 T ( n / 4 ) + n 2 T(n)=3T(n/4)+n^2 T ( n ) = 3 T ( n /4 ) + n 2 , what is the level-i i i work and the answer? n 2 ( 3 / 16 ) i n^2(3/16)^i n 2 ( 3/16 ) i ; ratio
< 1 <1 < 1 → root dominates →
Θ ( n 2 ) \Theta(n^2) Θ ( n 2 ) .
When does the root dominate vs the leaves? Root dominates when per-level work is a decreasing geometric series (
a < b d a<b^d a < b d ); leaves dominate when it's increasing (
a > b d a>b^d a > b d ).
Why does an unbalanced tree like T ( n / 3 ) + T ( 2 n / 3 ) + n T(n/3)+T(2n/3)+n T ( n /3 ) + T ( 2 n /3 ) + n give Θ ( n log n ) \Theta(n\log n) Θ ( n log n ) ? Each level sums to
≤ n \le n ≤ n and height is between
log 3 n \log_3 n log 3 n and
log 3 / 2 n \log_{3/2} n log 3/2 n , all
Θ ( log n ) \Theta(\log n) Θ ( log n ) .
Common wrong leaf count and the fix? Saying "
n n n leaves"; correct is
n log b a n^{\log_b a} n l o g b a (only equals
n n n when
a = b a=b a = b ).
Recurrence T(n)=aT(n/b)+f(n)
Work per level a^i f(n/b^i)
Intuition Hinglish mein samjho
Recursion tree ka matlab simple hai: jab tum koi recursive function likhte ho, toh wo apne ko baar-baar chhote problems mein todta jaata hai. Har call ko ek node maan lo, aur uss node ke andar likh do "is call ne kitna non-recursive kaam kiya" — yaani f ( n ) f(n) f ( n ) . Phir poore tree ke saare nodes ka kaam jod do, bas wahi tumhara total time T ( n ) T(n) T ( n ) hai. Itna hi concept hai.
Teen cheezein dhyaan rakhni hain: tree kitna deep hai (kitne levels — yeh log b n \log_b n log b n hota hai kyunki har level pe size b b b se divide hoti hai), har level pe kitne nodes hain (a i a^i a i , kyunki har node a a a bachche banata hai), aur har level ka total kaam (a i ⋅ f ( n / b i ) a^i \cdot f(n/b^i) a i ⋅ f ( n / b i ) ). Merge sort mein magic yeh hai ki har level ka kaam exactly n n n aata hai (kyunki 2 i 2^i 2 i aur n / 2 i n/2^i n / 2 i cancel ho jaate hain), aur log n \log n log n levels hain, isliye answer n log n n\log n n log n .
Sabse important trick: per-level kaam ko dekho — agar wo neeche jaate hue ghat raha hai (geometric series ratio < 1 <1 < 1 ), toh root jeet jaata hai, answer f ( n ) f(n) f ( n ) . Agar barabar hai toh ek extra log \log log factor lag jaata hai. Agar badh raha hai , toh leaves jeet jaate hain, answer n log b a n^{\log_b a} n l o g b a . Yeh exactly Master Theorem hai, par ratta maarne ki zaroorat nahi — har baar level sum se nikaal sakte ho.
Galti se bacho: log sochte hain "har level f ( n ) f(n) f ( n ) ka kaam karta hai" — galat! Sirf merge sort jaise balanced case mein aisa hota hai. Aur leaves ki ginti n n n nahi, n log b a n^{\log_b a} n l o g b a hoti hai. Yeh do baatein yaad rakh lo toh 80% problems clear.