Pages

Friday, June 6, 2025

Tower of Hanoi using Recursion in C++

 #include <iostream>

using namespace std;



// A to C using B 


void TOH(int n, char A, char B, char C) 

{

    if(n==0) {

        return;

        

    }

    

    

    TOH(n-1, A, C,B);

    cout << A << " to " << C<< endl;

    

    TOH(n-1, B, A,C);

}

int main()

{

    TOH(3, 'A', 'B', 'C');


    return 0;

}

No comments:

Post a Comment

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...