What is backtracking?
A systematic way to build solutions incrementally and abandon (backtrack from) a partial solution as soon as it can't be completed. It is DFS over an implicit state-space tree: nodes are partial solutions, edges are choices, and only the current root-to-node path is held in memory.
def subsets(nums):
res, path = [], []
def bt(i):
if i == len(nums):
res.append(path[:]) # snapshot, never the live list
return
bt(i + 1) # exclude nums[i]
path.append(nums[i]) # choose
bt(i + 1) # explore
path.pop() # unchoose
bt(0)
return res