Towers of Hanoi

If you've gone through the tutorial on recursion, then you're ready to see another problem where recursing multiple times really helps. It's called the Towers of Hanoi. You are given a set of three pegs and nn disks, with each disk a different size. Let's name the pegs A, B, and C, and let's number the disks from 11, the smallest disk, to nn, the largest disk. At the outset, all nn disks are on peg A, in order of decreasing size from bottom to top, so that disk nn is on the bottom and disk 11 is on the top. Here's what the Towers of Hanoi looks like for n=5n = 5 disks:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy