Recursion Algorithm | Tower Of Hanoi – step by step guide


Tower of Hanoi is a very famous game. In this game there are 3 pegs and N number of disks placed one over the other in decreasing size. The objective of this game is to move the disks one by one from the first peg to the last peg. And there is only ONE condition, we can not place a bigger disk on top of a smaller disk.

Tutorial and Code:

In this video we will learn to solve Tower Of Hanoi recursively by taking minimum number of moves.

0:03 About Tower Of Hanoi
0:18 Important rule to follow while solving Tower Of Hanoi
0:25 The three pegs of Tower Of Hanoi labeled A, B and C
0:33 The 3 disks in decreasing size from bottom to top
0:38 Objective of the game
0:51 How to solve Tower Of Hanoi
0:58 The general notation used to solve the Tower Of Hanoi problem recursively
1:15 The 3 steps to follow recursively to solve Tower Of Hanoi
1:50 Solving the game with 3 disks
2:05 Calculating the minimum number of moves to solve Tower Of Hanoi recursively
2:10 Solving T(3, A, B, C)
3:05 Solving T(2, A, C, B)
3:58 Solving T(2, B, A, C)
4:55 So we have the moves. Lets see them in action!
6:10 Solved!
6:15 Algorithm of Tower Of Hanoi (Recursive)
6:42 Calculating minimum number of moves to solve Tower Of Hanoi having N disks

Related Videos

You can download the code from my GitHub repository

To know more about Pseudo code kindly watch this video

Thanks 🙂


Xem thêm bài viết khác:


  1. Hi! I'm from Argentina. I tried to figure out my self this algorithm, but I couldn't. But I think I actually figured out the formula in the end, the one that returns the minimum of movements to solve a level. But my formula is different! (Also is less useful, but well I'm only a secondary student haha). Mine it's like this:

    T(n) = 2T(n-1) + 1

    T it's the "tower of disks" and n is the number on disks. For example, as you said, the minimum movements for a 3 disks tower is 7. For a tower of 4 disks, It will be 15 movements (7×2+1). For a tower of 5, 31 movements. 6 disks, 63 movements…. And so on. I discovered another things but I don't want to make really long comment so I will stop here :). Very good video, loved it!

  2. cant you just move the top one on the left to the close right each time, and when finished, move the top middle to the close right side everytime its solved isnt it?

  3. How is it you know you have made correct moves prior to the completion of the recursion? I'm thinking you put a lot of knowledge in your algorithm rather than letting the recursion do the work. Which I suppose is fine, but I would have preferred an exhaustive analysis that uses the recursion instead of using knowledge of the problem to solve it. Yes, this is more clever, and such, but it's not a great example of recursion, IMO.

  4. You are the only who explained well each step for the resolution of the Tower of Hanoi and how to get the moves. Nice video, thank you.

  5. Tq u so much sir. I've seen many videos of this game but no video is in detailed explanation as udid even without sounds


Please enter your comment!
Please enter your name here