1. Record node
Every node of the recursion tree is itself a valid subset.
result.push([...path])Typorithm · medium
Step 1 of 8 · intro card
Backtracking
Build the power set by recording every node of a choose / explore / undo recursion tree.
Every subset corresponds to a node in a recursion tree that grows by adding one later element at a time.
A start index ensures each element is only ever extended forward, so no subset repeats.
Push to choose, recurse to explore, then pop to undo — the universal backtracking shape.
Record the path at every node.
Loop from start to avoid duplicates.
Choose, recurse, then undo.
function subsets(nums) {
const result = [];
function backtrack(start, path) {
result.push([...path]);
for (let i = start; i < nums.length; i += 1) {
path.push(nums[i]);
backtrack(i + 1, path);
path.pop();
}
}
backtrack(0, []);
return result;
}Connect each part of the code to the concept before you type.
Every node of the recursion tree is itself a valid subset.
result.push([...path])Starting at start avoids reusing earlier elements and duplicate subsets.
for (let i = start; i < nums.length; i += 1)Add the current element to the path.
path.push(nums[i])Recurse with the next start so each element is used at most once.
backtrack(i + 1, path)Remove the element to restore state for the next choice.
path.pop()Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputValues 1,2,3
Follow temporary values, local state and supporting structures at this step.
Live recursive calls, including their arguments. Returning releases the top frame.
Current choice path0 entries
Local state · indices and saved values
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state