Tower of Hanoi
You have n disks stacked on rod A in decreasing size (largest at the bottom). Move the entire stack to rod C, using rod B as a helper rod , following the rules: move one disk at a time, only ever the top disk of a rod, and never place a larger disk on a smaller one. Return the sequence of moves as an array of strings like "A→C" . Use recursion : to move n disks from a source rod to a target rod, first move the top n − 1 disks from source to helper, then move the single largest disk from source straight to target, then move those n − 1 disks from helper to target.
Constraints
- 1 ≤ n ≤ 10
Example
n = 2["A→B", "A→C", "B→C"]Explanation Move the top disk out of the way to B, move the big disk straight to C, then move the small disk from B onto C.
In plain terms
- Helper rod
- A spare rod used as temporary storage while disks are moved between the source and target rods.
Move n − 1 disks aside, move the big disk, move them back
To move 2 disks from A to C, first move the top 1 disk from A to B (the helper).
What happens in this step
hanoi(2, A, C, B) calls hanoi(1, A, B, C) count = 2 is not the base case. First recurse: move the top 1 disk out of the way, from source A to helper B (using target C as the spare for that sub-move). This call must return before the big disk can move.
Steps to visualize
- hanoi(n, source, target, helper): if n is 0, there is nothing to move — base case, return immediately.
- Otherwise, first recurse: move the top n − 1 disks from source to helper, using target as the spare.
- Once that returns, move the single remaining disk directly from source to target.
- Then recurse again: move those n − 1 disks from helper over to target, using source as the spare.
- Each call does its own move sandwiched between two recursive calls — that's why the disk order comes out correct.
Walk through the code
Same walkthrough, now with the code. Press Next to move one step and watch which lines run.
To move 2 disks from A to C, first move the top 1 disk from A to B (the helper).
What happens in this step
hanoi(2, A, C, B) calls hanoi(1, A, B, C) count = 2 is not the base case. First recurse: move the top 1 disk out of the way, from source A to helper B (using target C as the spare for that sub-move). This call must return before the big disk can move.
Solution
function solveHanoi(n) {
const moves = [];
function hanoi(count, source, target, helper) {
if (count === 0) {
return; // base case
}
hanoi(count - 1, source, helper, target); // recursive case: clear the way
moves.push(`${source}→${target}`);
hanoi(count - 1, helper, target, source); // recursive case: bring them back
}
hanoi(n, 'A', 'C', 'B');
return moves;
}- Time
- O(2ⁿ)
- Space
- O(n)
Test cases
| Input | Expected | Covers |
|---|---|---|
n = 2 | ["A→B", "A→C", "B→C"] | example from the docstring |
n = 0 | [] | base case, no disks to move |
n = 1 | ["A→C"] | one disk moves straight from source to target |
n = 3 | ["A→C","A→B","C→B","A→C","B→A","B→C","A→C"] | the classic 7-move solution, several calls deep |
n = 4 | 15 moves following the same pattern twice, split by one direct move of the largest disk | move count doubles plus one with every extra disk (2ⁿ - 1) |