Easy · Include or Exclude
Understand → Visualise → Practice.
Every value is either included or excluded. That creates a binary decision tree with one leaf for each subset.
At index i, recurse once without nums[i] and once after adding it; remove it before returning.
Advance without changing the current subset.
Append the value and recurse.
Pop the value before exploring sibling branches.
O(n·2ⁿ) time to copy 2ⁿ subsets and O(n) recursion depth, excluding output.
Append a copy of the current list; otherwise all results refer to one mutable object.
Read the full guide first: Recursion and Backtracking.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.