Search code examples
algorithmstackcomplexity-theoryspace-complexity

Do recursive calls count into space complexity?


When an algorithm doesn't use more than a constant amount of auxiliary memory but does have O(log(N)) recursive calls (each one taking some extra space on the stack), is that algorithm's space complexity O(1) or O(log(N))?


Solution

  • If the recursive algorithm is not exploiting tail recursion, then, yes, a straightforward implementation would be using O(log(N)) space. This is because the runtime must keep O(log(N)) stack frames, each of O(1) size, in memory at once.