Recursion

AP Computer Science A· difficulty 3/5

fib(4) fib(3) fib(2) fib(2) fib(1) fib(1) fib(0)

The figure shows the call tree for naive <code>fib(4)</code>. Notice that

  • A

    Tree has constant depth

  • B

    No base cases

  • C

    Subproblems repeat (e.g., fib(2) computed multiple times) — exponential

    check_circle
  • D

    Each subproblem solved once

Explanation

Repeated subproblems make naive Fibonacci exponential; memoization fixes it.

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

Related questions