Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Fibonacci Series (Tree Recursion)

Medium Acceptance 60.00% Points 30.00

You are given a positive integer N. Your task is to compute the Nth Fibonacci number using tree recursion, where each function call makes two recursive calls to compute the previous two Fibonacci values.

The Fibonacci sequence follows:

F(0) = 0, F(1) = 1

F(n) = F(n-1) + F(n-2)

Return the value of the Nth Fibonacci number.

Examples
Example 1

Example 1

Input: N = 5

Output: 5

Explanation: 0 1 1 2 3 5 ? the 5th Fibonacci number is 5.

Example 2

Example 2

Input: N = 7

Output: 13

Explanation: 0 1 1 2 3 5 8 13 ? the 7th Fibonacci number is 13.

Example 3

Example 3

Input: N = 0

Output: 0

Explanation: Base case returns 0.

Hints
Hint 1
N/A
Constraints
  • 0 = N = 30
Companies
Microsoft
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions