# how many elements present in the stack when function is invoked

0

consider the following function:
int fibo(int a)
{
if(a==0)
return 0;
else if(a==1)
return 1;
return(fibo(a-1)+fibo(a-2));
}
how many elements present in the stack when the above function is invoked for fibo(7)?
a)12 b)11 c)35 d)40

Ravi Garg edited question

0
Mukul Taneja (anonymous)

yeah,,,,den what should be ur answer?

0
Mukul Taneja (anonymous)

over all 40….till the complete depth of recursion tree

0
Sumit Khatri (anonymous)

Mukul Taneja: but as you complete exploring left children and get base condition, you pop elements and then go to right side,….so it should not be 40………answer given is 40

0
Sumit Khatri (anonymous)

after we explore complete left children , we pop from stack(when base condition is reached), and then go for exploring right children….so, stack size will be changing every time and never reach value 40….at max, stack will be having 7 elements only…….if i am not wrong…

0
Mukul Taneja (anonymous)

see…it is right that stack will change every time but at max when the tree will at its max depth it will have total 40 elements..tht y i put “overall” there!!!!!!! draw a tree and then count all the elements till the last depth u will get it….:)