In this video lesson we will learn about recursion, how recursive algorithms work and we will code a classic problem in recursive manner - Factorial:
Problem - Factorial:
Write a recursive function that given a non-negative integer n, returns the factorial of n, also denoted as n!
n! = 1 * 2 * 3 * ... * (n - 1) * nExample:
Input: n = 5 Output: 120 Explanation: 5! = 1 * 2 * 3 * 4 * 5 = 120
Note:
Your algorithm should run in O(n) time and use O(n) extra space.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check