-1

If we don't consider stack call memory, then what is the space consumed by the recursive fibnonacci?

I read it here and it says 0(N) but I am confused whether we should include stack memory or not while considering the space.

The pseudocode:

int Fibonacci(int n)
{
   if ( n == 0 )
      return 0;
   else if ( n == 1 )
      return 1;
   else
      return ( Fibonacci(n-1) + Fibonacci(n-2) );
} 
Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98

1 Answers1

1

without considering stack call memory, It is again O(n) because you are passing a variable whose copy gets created in new function call, and this happens n times in n functions as maximum height of the recursion tree at any time is n or maximum level is n + 1 so asymptotically we can say it is O(n).

In case of bottom up approach again we use array to store the past value so that also becomes O(n) space(but in a clever way, we can make bottom up approach to work with only 3 variables which can be considered as O(1) space).

mss
  • 1,423
  • 2
  • 9
  • 18