🔄

Recursion Basics

Master recursive thinking, base cases, and the call stack before tackling trees and graphs.

0 Problems1-2 hours

Overview

Recursion is a function that calls itself to solve a smaller version of the same problem. Every recursive solution has a base case (the simplest input that can be answered directly) and a recursive case (breaking the problem into smaller pieces). Most tree and graph problems are naturally recursive.

Complexity Summary

Time Complexity

Depends on number of recursive calls and work per call

Space Complexity

O(d) where d is the maximum recursion depth

Key Patterns & Techniques

Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.

1

Base case

Concept

the stopping condition; without it the function runs forever

Practice questions for this pattern
2

Recursive case

Concept

call the same function with a smaller/simpler input

Practice questions for this pattern
3

Call stack

Concept

each call adds a frame; too many calls causes a stack overflow

Practice questions for this pattern
4

Trust the recursion

Concept

assume the recursive call works correctly on smaller input

Practice questions for this pattern
5

Dry run

Concept

draw the call tree to understand execution order

Practice questions for this pattern
6

Top-down thinking

Concept

define the function as "given this input, return this output"

Practice questions for this pattern
7

Memoization

Concept

cache results to avoid recomputing the same sub-problems

Practice questions for this pattern
8

Iteration vs recursion

Concept

any recursion can be converted to iteration using an explicit stack

Practice questions for this pattern
DSA Practice Online - 150+ Coding Interview Questions | LeetCode Alternative | InstaMock - AI Mock Interview