solution-space tree
[1,2,3]
[1,2]
[1,3,2]
[1,3]
[1]
[2,1,3]
[2,1]
[2,3,1]
[2,3]
[2]
[3,1,2]
[3,1]
[3,2,1]
[3,2]
[3]
[ ]
permutations
(none yet)
Backtracking · choose an unused element at each level
▸1permute(path, used):2 if path.length == n:3 record path // a complete permutation4 for num in nums where !used[num]:5 used[num] = true; path.push(num) // choose6 permute(path, used) // explore7 used[num] = false; path.pop() // un-choose (backtrack)
state
- input[1, 2, 3]