DSAverse
Recursion
Loading Recursion Visualizer...
Recursion Tree
depth: 1f(n) = f(n-1) + f(n-2)
Initializing Sorting Algorithms...
Sorting
Trees
Graphs
Preparing interactive visualizations...
Recursion
Loading Recursion Visualizer...
Recursion Tree
depth: 1f(n) = f(n-1) + f(n-2)
Watch the call stack grow as factorial calls itself, then see values return as the stack unwinds.
Adjust n and click Play to begin.
Adjust n and click Play to begin
Legend
Trace will appear here
What is the base case of factorial recursion?
def factorial(n):
# Base case: factorial of 0 or 1 is 1
if n <= 1:
return 1
# Recursive case: n * factorial(n-1)
return n * factorial(n - 1)
# factorial(5) = 120function factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
// factorial(5) = 120Exactly n recursive calls, each O(1). For n=5: 5 calls.
Call stack grows to depth n. For n=5: max 5 frames.