edited by
789 views
0 votes
0 votes
A(n)
{      
    if(n>=1)
     {
        A(n-1); // statement 1
        print n; //statement 2
        A(n-1);// statement 3
     }
}
edited by

1 Answer

Best answer
6 votes
6 votes
Size of stack is of the order of the number of recursive calls which are currently live. (It is not equal because we save many info on the stack during a call like local variables, return address etc. )

 Here, we have two recursive calls with value (n-1) for input n. But before the second one starts, the first one finishes. So, total number of recursive calls for n can be given by

Solve recurrence T(n) = 2T(n-1) + 1 (1 for the stack space required for the current process) with T(0) = 1.

and the number of live recursive calls (recursion depth) is given by

T(n) = T(n-1) + 1 which is O(n).
selected by

Related questions

2 votes
2 votes
0 answers
1
0 votes
0 votes
1 answer
2
gshivam63 asked May 21, 2016
469 views
find(int n) { if(x<2)return; else for(i =1; i<=4; i++) find (n/2); for(i= 1;i< =n*n; i++) sum=sum+1; }
0 votes
0 votes
1 answer
3
0 votes
0 votes
2 answers
4
gshivam63 asked Jun 2, 2016
1,812 views
Consider the following infix expression which is to be converted to postfix expression using stack.(((P+Q)*(R+S))/T)+(A*(B+C))