Visualize the exponential recursion tree of Fibonacci!
Each call branches into two more calls, causing exponential explosion.
function fib(n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}
Store computed values to avoid redundant calculations.
function fib(n, memo={}) {
if (n in memo) return memo[n];
if (n <= 1) return n;
memo[n] = fib(n-1,memo) + fib(n-2,memo);
return memo[n];
}