What is the space complexity of this code?

后端 未结 3 987
北海茫月
北海茫月 2021-01-12 00:46
int f(int n)
{ 
    if (n <= 1)
    { 
         return 1;
    } 
    return f(n - 1) + f(n - 1);
} 

I know that the time complexity is O(2

3条回答
  •  余生分开走
    2021-01-12 01:09

    Space complexity is O(n) because one side of recursion, reaches the leaves, and returns up, until the root, similar happens for the other side of recursion and in every middle step, the space used in the recursion cannot be bigger than O of depth of recursion tree.

提交回复
热议问题