Recursion
LockedLesson 22 of 60 · Syntax · Medium
Mastery0% of 85%
Up next: Variables and Primitive Types (~7 min) →Finish these first:Methods →Or pass the tests below to unlock it now.
1
The big idea
Recursive methods need a base case and a shrinking subproblem. The JVM has no tail-call optimization; very deep recursion throws StackOverflowError.
Think of it like this: Think of syntax and expressions like labeled measuring cups in a kitchen: each cup holds a specific kind of ingredient, and operators combine them into a new measurement.
Key words (tap to learn):
2
Read the lesson
Recursive methods need a base case and a shrinking subproblem. The JVM has no tail-call optimization; very deep recursion throws StackOverflowError.
static long fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); }
3
Quick check
Before writing code: What should `Solution.pow2(0)` return when your implementation of Recursion is working?
4
2 sample tests, 1 hiddenYour challenge
Write static int pow2(int n) computing 2^n recursively.
Stuck? Get a hint (free)
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
Similar to python.recursion (95% overlap). Same idea; neither optimizes tail calls.
Visual walkthrough
A diagram, a step-by-step animation, and short captions for Recursion.
Mastery0%
Learn this first
- MethodsNot yet