Recursion & Backtracking
IntermediateAlgorithms
Backtracking systematically explores all possibilities by building candidates incrementally and abandoning (pruning) paths that cannot lead to a solution. Essential for permutations, subsets, and constraint satisfaction.
AVG TIME
O(2^n)
SPACE
O(n)
BEST
O(n!)
WORST
O(n!)
Key Concepts
- 1Backtracking template: choose, explore, unchoose
- 2Subsets: at each element, decide to include or exclude
- 3Permutations: swap elements with remaining positions
- 4Pruning early is critical for performance
- 5State must be fully restored after each recursive call
In Python
Math You Need For This
A maze where you try every path. When you hit a dead end you backtrack to the last fork and try a different direction. Stack space = how deep you are in the maze, not the total number of paths.
Required concepts
Key math ideas
1 / 3
Interactive 3D Visualization
Python Implementation
Now try it yourself
1 challenge with test cases and AI feedback
Practice Now
Complexity Analysis
Recursion & Backtracking
Next: Dynamic Programming