π Backtracking
Backtracking is DFS through a decision tree. Choose, explore, unchoose. If you can describe the problem as "build a solution piece by piece, backtracking when stuck", this is the right tool.
This category contains 16 problems. Use the patterns below to recognize what's being asked, then jump to the problem list at the bottom.
π§ Key Patternsβ
- Subsets / Power Set β Include or exclude each element.
- Permutations β Used-set tracking; swap-in-place variant for extra space.
- Combinations / Combination Sum β Start index parameter prevents repeats.
- Word Search / Path in Grid β Mark cell visited, recurse, unmark.
- N-Queens / Sudoku β Constraint-satisfaction with placement validation.
- Partitioning β Palindrome partitioning, IP addresses β try every split point.
β οΈ Common Pitfallsβ
- Forgetting to "unchoose" β results pollute across branches.
- Copying the current state into results without deep-copying (
new List<int>(current)). - Missing pruning conditions β TLE on Hard problems.
π Study Resourcesβ
πΊ Videosβ
π Booksβ
- Algorithms β Erickson (free) β Ch. 2 β backtracking with intuition: https://jeffe.cs.illinois.edu/teaching/algorithms/
- Introduction to Algorithms (CLRS) β (brief mention in advanced topics)
π Articles & Referencesβ
π» All Backtracking Problemsβ
Combination Sum
LeetCode 39 | Difficulty: Medium
Combination Sum II
LeetCode 40 | Difficulty: Medium
Combination Sum III
LeetCode 216 | Difficulty: Medium
Combinations
LeetCode 77 | Difficulty: Medium
Generate Parentheses
LeetCode 22 | Difficulty: Medium
Letter Combinations of a Phone Number
LeetCode 17 | Difficulty: Medium
N-Queens
LeetCode 51 | Difficulty: Hard
N-Queens II
LeetCode 52 | Difficulty: Hard
Next Permutation
LeetCode 31 | Difficulty: Medium
Permutations
LeetCode 46 | Difficulty: Medium
Permutations II
LeetCode 47 | Difficulty: Medium
Permutations II
Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
Restore IP Addresses
LeetCode 93 | Difficulty: Medium
Subsets
LeetCode 78 | Difficulty: Medium
Subsets II
LeetCode 90 | Difficulty: Medium
Word Search
LeetCode 79 | Difficulty: Medium