Recursion and Backtracking Basics
Lesson 8 of 20Watch the call stack pile up and unwind digitSum frames — then follow fib(4) through every branch of its recursion tree.
The big picture
Recursion is a method that calls itself on a smaller piece of the problem. The base case stops the descent; the call stack assembles the answer on the way back up.
A method that calls itself
digitSum(472) peels off one digit (472 % 10 = 2) and hands 47 to a fresh copy of itself — same method, smaller problem.
The base case is the brakes
A single digit (n < 10) is answered directly with no further calls. Miss the base case and Java throws StackOverflowError, not an infinite loop.
The JVM call stack pays
Every call pushes a frame holding its own n. digitSum(472) stacks 3 frames — depth grows with the input, one frame per level.
The unwind
Answers are assembled on the way back up: 4 returns, then 7 + 4 = 11, then 2 + 11 = 13. Work after the call runs in reverse order.
Backtracking = try, recurse, undo
Listing subsets of {4, 9} branches at every item — take it or skip it — 2 choices squared is 4 outcomes, and each choice is undone before trying the other branch.
Naive recursion repeats work
fib(4) makes 9 calls and computes fib(2) twice. fib(30) would make over 2.6 million calls — memoization (a later topic) fixes that.
Call stack at work
digitSum(472) in flight — frames pile up until the base case, then pop off one by one.
main() is running — the JVM call stack holds exactly one frame.
The recursion flow
Every call enters at the top and asks the same question with a smaller n — the loop-back edge IS the recursion.
Walkthrough
Two frames-of-mind programs: digitSum unwinding, then a subset picker trying and undoing choices.
1public class DigitSum {2static int digitSum(int n) {3if (n < 10) return n;4return n % 10 + digitSum(n / 10);5}67public static void main(String[] args) {8System.out.println(digitSum(472));9System.out.println(digitSum(9));10System.out.println(digitSum(1000));11}12}
First call: digitSum(472) — a frame opens with n = 472.
Fibonacci tree
fib(4) unfolds into a tree of calls — watch answers bubble back up the branches.
fib(4) is called. It is not 0 or 1, so it cannot answer alone — it must ask fib(3) and fib(2).
Predict the output
Lines after the recursive call run on the way back up — trace the stack, not just the code.
1public class Echo {2static void echo(int n) {3if (n == 0) {4System.out.println("liftoff");5return;6}7System.out.println("down " + n);8echo(n - 1);9System.out.println("up " + n);10}1112public static void main(String[] args) {13echo(2);14}15}
Hint: echo(0) hits the base case and prints first. Then each frame finishes its last line in reverse order — echo(1) before echo(2), each with its own n.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
Every recursion needs a base case — it is the only thing that stops the calls.
Each call pushes a frame with its own parameter copies; depth = how many frames stack up.
Work placed after the recursive call runs on the way back UP, in reverse order.
A missing base case means StackOverflowError — Java does not loop forever, it crashes.
Backtracking is try → recurse → undo: revert the choice before exploring the next branch.
Naive fib recomputes the same subtrees — fib(4) already calls fib(2) twice, and the cost grows as O(2ⁿ).