Stepping Through Recursive Fibonacci Function

Khan Academy · 233K views · 2011 · 8:04

Open on YouTube

Education & learningAlgorithms & data structurestutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering beginners who are confused about how recursion and the call stack evaluate.

Recommended by 3 people across 3 different months, 2014 to 2021.

What it is

A short Khan Academy walkthrough that steps through the call tree of a recursive Fibonacci function to show how and why it works. It is part of Khan Academy's introductory computer science material.

What people said

Recommends Jonn · 2014-05 · Stack Overflow · on “Java recursion reverse string”

Let's work through the example a bit: You start by calling reversePrint("abcdef") . To abbreviate, I'll just write this as rev(abcdef) . rev(abcdef) = rev(bcdef) a // Take the beginning (a) and put it on the end. = (rev(cdef) b) a = ((rev(def) c) b) a = (((rev(ef) d) c) b) a = ((((rev(f) e) d) c) b) a = fedcba At each step, we evaluate rev on a substring of the original. So first we evaluate rev(abcdef) . But to work that out, we need to evaluate rev(bcdef) , and for that we need rev(cdef) and so on. We work this out all the way down to rev(f) , which is just f . Then we concatenate one string onto the next, and we end up with rev(abcdef) = fedcba . I'd recommend watching Khan Academy's vide

Recommends Jonn · 2014-05 · Stack Overflow · on “Java recursion reverse string”

@user3545850 No problem. Make sure you take a look at this video on recursion [this video] if you're still having trouble.

Recommends Lasse V. Karlsen · 2018-09 · Stack Overflow · on “return not exiting recursive method as expected - C#”

If that is actually your question you should just find a tutorial that shows how a recursive Fibonacci sequence implementation works, as that is the easiest to deal with and explain, it should explain the part about the stack and so on just fine, like this one: [this video]

Recommends jarmod · 2021-11 · Stack Overflow · on “Can anyone explain recursive in Nth fibonacci?”

I'd recommend the Khan Academy video [this video] . The code you've shown includes a hash table for memoization which makes the solution a little more complex than usual, but if you understand the video, you'll understand the basic code. After that, you can read about memoization.