PAVANXDCL • DSA FOUNDATION

Master Recursion

Learn how a function can solve a smaller version of the same problem. Understand base cases, recursive calls, the call stack and important recursion patterns.

01 — FOUNDATION

What is Recursion?

Recursion is a technique where a function calls itself to solve a smaller version of the same problem.

↻

Base Case

The condition that stops the recursive calls. Every recursive solution needs a clear stopping condition.

↓

Recursive Case

The function calls itself with a smaller or simpler input.

▤

Call Stack

Every function call waits on the stack until its recursive call finishes.

02 — HOW IT WORKS

Think in Smaller Problems

A recursive problem can usually be understood using three simple steps.

1. Base Case When should recursion stop?
→
2. Recursive Call Move toward the base case
→
3. Combine Build the current answer
03 — EXAMPLE

Factorial Recursion

Factorial is one of the easiest ways to understand the recursive idea.

factorial(n) = n × factorial(n - 1)

factorial(0) = 1
factorial(4) 4 × factorial(3)
→
factorial(3) 3 × factorial(2)
→
factorial(2) 2 × factorial(1)
→
factorial(0) Base case
factorial(0)
return 1
factorial(1)
1 × 1 = 1
factorial(2)
2 × 1 = 2
factorial(3)
3 × 2 = 6
factorial(4)
4 × 6 = 24
04 — PATTERNS

Important Recursion Patterns

These patterns appear frequently in DSA problems.

Linear Recursion

One recursive call at every step. Examples include factorial and sum of numbers.

Multiple Recursion

A function makes multiple recursive calls. Fibonacci is a common example.

Array Recursion

Use an index or two pointers to recursively process an array.

String Recursion

Move through a string or reduce it one character at a time.

Backtracking

Make a choice, recurse, then undo the choice and try another possibility.

Divide & Conquer

Divide a problem into smaller parts, solve them recursively and combine the results.

05 — THINKING PROCESS

How to Solve a Recursion Problem

01

Define the Function

Decide exactly what the function should return or accomplish.

02

Find the Base Case

Ask when the problem becomes small enough to solve directly.

03

Make Progress

Every recursive call must move closer to the base case.

04

Trust the Call

Assume the recursive function correctly solves the smaller problem.

05

Combine

Use the smaller result to construct the answer for the current problem.

06

Trace

Draw the call stack when recursion becomes difficult to visualize.

06 — COMPLEXITY

Recursion Complexity

Problem Time Stack Space
Factorial O(n) O(n)
Sum of Array O(n) O(n)
Binary Search O(log n) O(log n)
Merge Sort O(n log n) O(log n)
Naive Fibonacci O(2ⁿ) O(n)
07 — CODE

Write Recursive Code

Implement factorial using recursion.

C++ — recursion.cpp
Sample tests have not been run.
08 — VISUALIZE

Trace a Recursive Call

Enter a number and see how factorial moves toward the base case.

09 — VIDEO LEARNING

Recursion Videos

Watch the concept first and then solve the practice problems.

▶
BEGINNER

Recursion Basics

Base cases, recursive calls and the call stack.

▶
CORE

Merge Sort Part 1

Anaysing depth of recursion and time complexity.

▶
INTERVIEW

Merge Sort Part 2

Anaysing depth of recursion and time complexity.