Fireship · @fireship

Recursion in 100 Seconds

Uploaded December 30, 2019 · Length 1:40

Recursion in 100 Seconds

Summary

How to implement recursion in programming with examples and use cases.

  • recursion
  • programming
  • algorithms
  • functions
  • tree traversal
  • graph traversal

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    a recursive function is a function that

  2. 0:02

    calls itself inside of its own

  3. 0:04

    definition it's like having a dream

  4. 0:06

    inside of a dream when you have a

  5. 0:07

    recursive function that calls itself

  6. 0:09

    without anything to stop it you have

  7. 0:11

    created an infinite Loop the first

  8. 0:13

    function gets pushed onto the call stack

  9. 0:14

    the next one on top of it then another

  10. 0:16

    one on top of that and so on forever or

  11. 0:19

    until your runtime throws a stack

  12. 0:20

    Overflow error or the computer just runs

  13. 0:22

    out of memory so recursion is just

  14. 0:24

    another way to create a loop but

  15. 0:26

    infinite Loops aren't very useful in

  16. 0:28

    practice that's why recursive function

  17. 0:30

    have some sort of stopping point like

  18. 0:32

    some conditional logic that tells the

  19. 0:33

    function to stop calling itself when a

  20. 0:35

    condition has been met on an interview

  21. 0:37

    you might be asked to implement some

  22. 0:38

    code that finds a specific index in the

  23. 0:41

    Fibonacci sequence you'll notice a given

  24. 0:43

    index of the sequence is just the sum of

  25. 0:45

    the two previous values so how do we

  26. 0:47

    find index number 2023 your first

  27. 0:50

    thought might be to implement an

  28. 0:51

    iterative solution with a for Loop

  29. 0:53

    compute the Fibonacci value for each

  30. 0:54

    index until we get to the Target index

  31. 0:56

    of 2023 but then you're asked to

  32. 0:59

    implement a cursive solution we can do

  33. 1:01

    that by writing a function that starts

  34. 1:03

    with the index and then Works its way

  35. 1:04

    backwards until it gets to a base case

  36. 1:07

    of being less than or equal to one

  37. 1:09

    notice how we're calling the same

  38. 1:10

    function inside the function body and

  39. 1:12

    we're doing it twice there's a big

  40. 1:14

    problem here though it will Traverse

  41. 1:15

    down the entire sequence for every

  42. 1:17

    single index that's o to the N squ time

  43. 1:20

    complexity which is not good at all in

  44. 1:21

    the next video I'll explain how to

  45. 1:23

    improve this with memorization recursive

  46. 1:25

    functions are not the right choice in

  47. 1:26

    every situation but they are very good

  48. 1:28

    for tree and graph Travers rals this has

  49. 1:30

    been recursion in 100 seconds thanks for

  50. 1:33

    watching and I will see you in the next

  51. 1:35

    one