Can you please explain if (n = 3) line by line How code works.
I didn’t get it ??
When n=3 code runs then first recursive call is done then n=2 runs then again recursive call is done and n=1 then after ??
Tower of Hanoi Problem
@ashwani225 Dont try to understand it recursion call. It will be more complicated then.
Just understand it like this. There is function which take 4 inputs
- Rod you wanna move
- Source Rod
- Destination Rod
- Helper Rod
and all other work done by that function you dont know how it is working but it will do you work.
Next step is what you will write in that function.
there is 3 steps
- Move the rod which is above of (nth) rod which is (n-1)th to helper rod
- Now Move (nth) rod to destination rod.
- Last step is to move back that (n-1)th road to source node
and if n==1 which is the smallest rod so there is no (n-1)th rod so you will just move it to destination rod without any helper rod and that is the base case of function.
I hope you get this.
Ping me back if you didnt get it.
I get that but only I want to ask how No is increasing after when It have decresed?
@ashwani225 when recursion return from base condition then it follow the step 2 and 3 of algo due to which that disk with higher number moves.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.