
Recursion and iteration are two ways to repeat work. Recursion repeats by having a function call itself on a smaller version of the problem until it reaches a base case. Iteration repeats a block of instructions with a loop until a condition changes. They can often solve the same task, but recursion uses nested call frames, while iteration usually keeps its progress in loop variables.
Neither is universally better. Iteration is usually the practical choice for simple linear repetition because it avoids call-stack growth. Recursion can be clearer when the data or problem is naturally recursive, as with trees, divide-and-conquer algorithms, and nested structures. The best choice is the one that makes the control flow correct, readable, and safe for the expected input depth.
Both techniques need a way to make progress and a way to stop. A loop changes a counter, pointer, or collection until its condition becomes false. A recursive function changes its arguments so each call moves closer to a base case. If neither mechanism moves toward stopping, the result is an infinite loop or runaway recursion.
Factorial makes the contrast visible. Five factorial, written 5!, is 5 × 4 × 3 × 2 × 1 = 120. The Python functions below reject negative inputs and agree on 0! = 1.
def factorial_recursive(n): if n < 0: raise ValueError("n must be non-negative") if n <= 1: return 1 return n * factorial_recursive(n - 1)
def factorial_iterative(n): if n < 0: raise ValueError("n must be non-negative") result = 1 for value in range(2, n + 1): result *= value return result
Calling factorial_recursive(5) creates calls for 5, 4, 3, 2, and 1. The deepest call returns 1; then the waiting calls multiply as the stack unwinds: 2, 6, 24, and finally 120. The iterative version starts with result = 1 and updates it four times as value runs from 2 through 5. Both perform a linear amount of multiplication.

Every active recursive call needs a frame containing information such as its arguments, local variables, and return location. For factorial_recursive(5), five factorial calls are active before the base case returns. For factorial_recursive(n), the number of simultaneously active factorial frames grows with n, so its auxiliary stack space is O(n).
The factorial loop reuses a small fixed set of variables, so its auxiliary space is O(1). That does not mean iteration is always constant-space: an iterative depth-first tree traversal normally uses an explicit stack, which can grow with the tree. Iteration often moves the bookkeeping out of the runtime call stack and into a structure you manage yourself.
Python deliberately has a recursion-depth limit to protect its interpreter stack. The exact limit is an implementation setting, so code should not assume one universal number. Raising it can be unsafe; for potentially deep input, an iterative design is usually more robust.

A binary tree is recursive by shape: each node has a value and smaller left and right trees. A preorder traversal therefore reads naturally as visit this node, traverse the left subtree, then traverse the right subtree.
def preorder_recursive(node): if node is None: return [] return [node.value] + preorder_recursive(node.left) + preorder_recursive(node.right)
def preorder_iterative(root): order = [] stack = [root] while stack: node = stack.pop() if node is None: continue order.append(node.value) stack.append(node.right) stack.append(node.left) return order
The recursive version lets the runtime remember where to return after each subtree. The iterative version makes that memory explicit. It pushes the right child before the left because a stack is last in, first out, so the left child is processed first. Both versions take O(n) time for n nodes and O(h) extra space for a tree of height h in the typical case. A severely skewed tree can make h as large as n.
A common misconception is that recursion is automatically slower than iteration. Function calls and stack frames often add overhead, but real performance depends on the language, compiler, runtime, algorithm, and input. Another misconception is that every recursive function can be converted into a neat loop. Recursion can be simulated with an explicit stack, but the iterative code may be longer and no more memory-efficient.
The base case. It handles an input directly without making another recursive call. The recursive step must also move the input closer to that base case.
Each call waits for the smaller factorial to return, so multiple call frames remain active. The loop updates the same accumulator and counter instead.
Its control flow mirrors the tree's nested structure, which can make the algorithm shorter and easier to reason about. Use iteration when depth may exceed safe recursion limits or when you need direct control over traversal state.
If recursion and iteration still blur together in your course notes, Snitchnotes can turn your lectures, slides, and PDFs into clearer study notes, flashcards, quizzes, and audio for practice.
Massachusetts Institute of Technology, 6.031: Recursion
Cornell University CS 2110: Recursion
Notizen, Quizze, Podcasts, Karteikarten und Chat — aus einem Upload.
Erste Notiz kostenlos testen