P-5.2 Recursion, and Trusting It
You can write a correct recursive solution by identifying a base case and a smaller subproblem, and explain when recursion costs more than the equivalent loop.
Recursion is hard to learn for one reason: people try to trace it. The working method is the opposite, which is to assume the function already solves smaller cases correctly and to write only the step that reduces the problem. Once that habit is in place, trees, sorting and searching all become short. The cost to keep in mind is the call stack, which is finite and which naive recursion on large inputs will exhaust.
Work through these
A base case and a step that makes the problem smaller
Those two pieces are the entire recipe, and a missing or unreachable base case is what produces infinite recursion. Write the base case first every time.
Algorithms · ReferenceAssume the smaller call is already correct, and write only one step
Trusting the recursive call rather than tracing it is the mental move that makes this workable. Tracing three levels down to convince yourself is how people conclude recursion is hard.
Recursion on trees, where the structure is already recursive
A tree is a node with subtrees, so a function that handles a node and calls itself on the children is a direct restatement of the definition. Nothing shorter exists.
Each call costs a frame, and the stack has a limit
Depth costs memory, so recursion proportional to input size will overflow on large inputs. This connects directly to the call stack from the data structures module.
When a loop is the better answer, and what tail recursion means
Simple linear recursion is often clearer as a loop and always cheaper. Some languages convert a call in the final position into a loop for you, and knowing whether yours does is worth checking.
Sign in to keep your progress.
Free resources
Links last checked 31 Aug 2026.
Stuck here?
Ask a mentor. A real person answers, and they can see exactly which topic you're on. Usually within a couple of working days.
Checking your session…
Topics shown in module order.