visualization
Backtracking: Subsets
Choose, explore, un-choose — the undo is what makes it work.
nums = [1, 2, 3] · enumerate all 2³ = 8 subsetsstep 1/23
current path = [] · recorded subsets
{}
Enter the root with an empty path — record {} first; the empty set is a subset too.
pseudocode
def backtrack(start, path):record(path[:]) # every node is a subsetfor i in range(start, len(nums)):path.append(nums[i]) # CHOOSEbacktrack(i + 1, path) # EXPLOREpath.pop() # UN-CHOOSE# 2^3 = 8 subsets recorded