1. Concept library
  2. Data Structures & Algorithms
  3. Module 6: Recursion and backtracking

Concept

Base cases and the call stack

A recursive function calls itself on a smaller input until a base case stops it. Each call sits on the call stack until it returns, which is why recursion that is too deep fails even when it is correct.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what a base case is and why every call must move toward one
  2. Write a recursive function and trace its calls on a small input
  3. Spot the recursion that never reaches its base case

In practice

A few lines that show Base cases and the call stack doing its job.

Python
def depth(node):
    if node is None:                 # base case
        return 0
    return 1 + max(depth(node.left), depth(node.right))

def factorial(n):
    return 1 if n <= 1 else n * factorial(n - 1)

import sys
sys.getrecursionlimit()              # 1000 by default

The common mistake

Watch for

Writing the recursive case first and the base case as an afterthought, or on an input the recursion never reaches. Every call must move strictly toward a base case that returns without recursing.

What to know first

Your roadmap puts these before Base cases and the call stack, unless you already know them.

How it stays learned

Once a session shows you understand Base cases and the call stack, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Base cases and the call stack.

Next in the moduleBacktracking templates →