Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Recursion

Рекурсия

A function uses recursion when it solves a problem by calling itself on simpler cases of the same problem. It stops calling itself once it reaches a case it can answer directly.

When recursion fits

Suppose you need to count the files in a folder and all its subfolders. You can count the files directly in the current folder, then use the same procedure to count the files in each subfolder. A folder with no subfolders needs no further calls. The recursive structure comes from the data: each subfolder can be treated as a folder in its own right.

What happens during a call

A recursive function needs a base case, which returns an answer without recursing, and a recursive step, which makes progress toward a base case. Progress might mean cutting a search range in half, moving to a child node, or reducing the number of items left to process. A stopping condition is useful only if the calls actually reach it.

In a typical implementation, each active call has a frame on the call stack. The frame holds information needed to resume the call, such as its parameters and where execution left off. When a nested call returns, the caller can use its result and continue. That is why the total number of calls and the maximum number active at once are two different quantities.

Example: count the nodes in a binary tree

Each node has a left and a right child reference. A missing child is represented by None.

def count_nodes(node):
    if node is None:
        return 0
    left_count = count_nodes(node.left)
    right_count = count_nodes(node.right)
    return 1 + left_count + right_count

For a leaf, both child calls return 0, so the function returns 1. For a larger tree, each child call counts its own subtree, and the final 1 counts the current node. Counting an n-node tree takes O(n) time. Extra call-stack space is O(h), where h is the tree height: the left call finishes before the right call starts, so only one root-to-child chain is active at a time.

When to use a loop or a cache instead

Recursive code often reads naturally for tree traversal, nested data, and divide-and-conquer algorithms such as merge sort. A loop is usually simpler when processing a flat list from start to finish. For a traversal whose depth can grow with the input, a loop with an explicit stack avoids the runtime's recursion-depth limit. It still uses memory for pending work; it just manages that work as data rather than as nested calls.

Watch for repeated work as well as deep calls. A naive recursive Fibonacci function calculates the same values many times. Memoization caches those results so they can be reused. It reduces repeated work, but does not by itself guarantee shallow recursion.

What interviewers look for

State what one call promises to return before writing the recursive step. Then trace an empty input and a small nonempty input to check that the step makes progress and combines results correctly. When discussing complexity, count all the work for the time bound and the peak number of active frames for the call-stack space bound.