float foo(int n){
if(n <= 2) return 1;
else return (2*foo(n-1) + 3*foo(n-2));
}
Instead of expanding the entire call tree and counting the foo(2) nodes by hand, we can define a helper count function c(n) equal to the number of times foo(2) gets called while evaluating foo(n), and work out a recurrence for c(n) itself.
By inspecting the code, foo(n) for n>2 makes exactly two recursive calls, to foo(n-1) and foo(n-2); any call to foo(2) that happens during the evaluation of foo(n) must come from inside one of those two recursive branches, plus one extra count if n itself equals 2 (since then the call to foo(n) is itself a call to foo(2)). This gives:
\[ c(n) = c(n-1) + c(n-2) + [n=2], \qquad c(1)=0 \]Building up from the smallest cases:
\[ c(1) = 0, \quad c(2) = 1, \quad c(3) = c(2)+c(1) = 1, \quad c(4) = c(3)+c(2) = 2, \quad c(5) = c(4)+c(3) = 2+1 = 3 \]Building the count recurrence up from the base cases confirms foo(2) is called exactly 3 times while evaluating foo(5).
Therefore, the correct answer is 3.