AlgoPrecision

Backtracking

Decision Tree Visualizer

Step through choose, explore, reject, and undo moments while the path, board state, result count, time, and space complexity update.

O(n!) time / O(n) space

Choices

Choice1
Choice2
Choice3

Current Path

empty

Results

No result yet

Results

0

Time

O(n!)

Space

O(n)

Step Note

Build each ordering by choosing unused values.

Details
function permutations(values: number[]) {
  const result = []
  function dfs(path: number[], used: Set<number>) {
    if (path.length === values.length) result.push([...path])
    for (const value of values) {
      if (used.has(value)) continue
      used.add(value)
      path.push(value)
      dfs(path, used)
      path.pop()
      used.delete(value)
    }
  }
  return result
}