Subsets (Power Set)
Return all subsets (the power set) of an array of distinct integers — 2ⁿ of them.
Open on LeetCode ↗Intuition
Every subset is a chain of n independent yes/no decisions: include this element or not. Backtracking walks that decision tree — take the element, recurse, un-take it, recurse — and every leaf is one subset. Equivalently, each n-bit number 0..2ⁿ−1 is a subset: bit i set means take nums[i].
Approach
Include / exclude recursion
dfs(i, path): at each index, branch on taking nums[i] or skipping it; at i == n, snapshot path. 2ⁿ leaves = 2ⁿ subsets.
Snapshot every node instead
The common variant records path on entry and only branches on which later element to add next — same output, no explicit base case.
Bitmask alternative
for mask in range(2**n): build the subset from set bits. Iterative, no recursion, same O(n·2ⁿ).
Solution & live demo
Edge cases
Power set is [[]] — one empty subset.
The all-skip and all-take branches produce them.
Append path[:], not path — the list mutates during backtracking.