Recursion

AP Computer Science A· difficulty 4/5

f(4) f(3) f(2) f(2) f(1) f(1) f(0)

Given:

<code class="language-java">public static int f(int n) { if (n <= 1) return n; return f(n - 1) + f(n - 2); } </code></pre> Using the partial call tree shown, what is <code>f(4)</code>?

  • A

    5

  • B

    4

  • C

    3

    check_circle
  • D

    2

Explanation

This is Fibonacci: f(0)=0, f(1)=1, f(2)=1, f(3)=2, f(4)=f(3)+f(2)=2+1=3.

Want 10 more like this — adaptive to your weak spots?

Related questions