Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Recursion

    Solve a problem by solving a smaller copy of it — the call stack, base cases, recursion trees and when to use loops instead.

    • Module 11 of 11
    • Intermediate
    • 9 chapters
    • 48 min

    Recursion powers trees, graphs, backtracking and dynamic programming — a large share of DSA problems. Seeing how it runs on the call stack makes it predictable.

    What you will be able to do

    • Design a recursive method from a base case and a smaller step
    • Trace recursion on the call stack
    • Solve problems with backtracking and memoization
    • Turn recursion into a loop when depth is a risk

    Chapters

    1. The IdeaA recursive method is a method that calls itself on a smaller version of the problem.4 min2. How It Actually Runs: The Call StackWinding (going down): each call pauses at factorial(n - 1) and pushes a new frame.4 min3. Designing a Recursive MethodA manager given a huge job doesn't do it all. They do one small piece themselves and hand "the rest" to an assistant — who does the same thing. The manager6 min4. Recursion on Structures: Nested FoldersA folder can contain notes and other folders. A folder is defined in terms of folders. When a structure is defined by containing smaller copies of itself, the4 min5. Shapes of RecursionRecursive binary search — the halving shape (the full technique is in 02DSA/01Concepts/07binarysearch.md):3 min6. Backtracking: Exploring Every ChoiceWalking through a maze with a piece of chalk. At each fork, choose a path and mark it. Walk until you reach the end or a dead end. At a dead end, walk back5 min7. Memoization: Don't Solve the Same Small Problem TwiceSome recursions reach the same smaller problem many times — overlapping subproblems. Naïve Fibonacci was the example (06bigocomplexity.md Ch 8).3 min8. Cost, Depth and Turning Recursion Into a Loopdepth, not on the total number of calls — naïve stairs(40) makes 331 million calls but is never more than 40 frames deep.4 min9. The Classic BugsA debugging technique that always works: print the arguments on entry and the return value on exit, with indentation by depth — exactly the trace in Chapter 2.2 min

    Extras