Two backtracking shapes look similar but answer different questions. Knowing which you need — and how their decision trees differ — is half the battle.
A subset is a choice of which elements to include; order doesn't matter. For each element you make a binary decision: in or out.
[1,2,3] → {}, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3} (8 of them).// include/exclude each element
function subsets(i, current):
if i == length(nums): record(current); return
subsets(i+1, current) // exclude nums[i]
subsets(i+1, current + [nums[i]]) // include nums[i]
A permutation uses all elements but in some order; order is the whole point. At each slot you choose which unused element goes next.
[1,2,3] → 123, 132, 213, 231, 312, 321 (6 of them).function permute(current, remaining):
if length(remaining) == 0: record(current); return
for each elem in remaining:
permute(current + [elem], remaining without elem)
A combination is a subset of a fixed size k — choose exactly k elements, order
irrelevant ({1,2} and {2,1} are the same combination). It sits between subsets and
permutations: like subsets, order doesn't matter; unlike subsets, the size is pinned.
[1,2,3]
gives {1,2}, {1,3}, {2,3} (3 of them).Same backtracking skeleton, different branching rule. Spot which question is being asked and pick the matching tree.