Skip to content

Commit ad20963

Browse files
authored
Update 01 - Recursive Approach.cpp
1 parent 797f1f6 commit ad20963

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
class Solution {
22
public:
33
int nthFibonacci(int n) {
4-
if(n == 0 || n == 1) return n;
5-
6-
return nthFibonacci(n-1) + nthFibonacci(n-2);
4+
// Base case: If n is 0 or 1, return n directly.
5+
// F(0) = 0 and F(1) = 1 are the first two terms of the Fibonacci sequence.
6+
if (n == 0 || n == 1)
7+
return n;
8+
9+
// Recursive case:
10+
// The nth Fibonacci number is the sum of the (n-1)th and (n-2)th Fibonacci numbers.
11+
// Recursively calculate the values for smaller subproblems.
12+
return nthFibonacci(n - 1) + nthFibonacci(n - 2);
713
}
814
};

0 commit comments

Comments
 (0)