Math Fundamentals18 sections · 814 units
Open in Course

Logarithmic Space Complexity (Recursion depth)

Call stack usage

Recursive algorithms that halve the problem size use O(logn)O(\log n) space for the call stack. For example, binary search's recursive version has recursion depth O(logn)O(\log n).

Each recursive call adds a stack frame. With log2(n)\log_2(n) levels of recursion, you use O(logn)O(\log n) stack frames, each holding a constant amount of data.

You can often convert recursive solutions to iterative ones to reduce space from O(logn)O(\log n) to O(1)O(1), but the time complexity stays the same.