jain12

number of BST possible for n nodes

Mar 30th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.34 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int TotalBSTPossible(int n){
  5.   if(n<=1)
  6.     return 1;
  7.   int sum=0;
  8.   for(int root=1;root<=n;root++){
  9.    int left=TotalBSTPossible(root-1);
  10.    int right=TotalBSTPossible(n-root);
  11.    sum+=left*right;
  12.    }
  13.   return sum;
  14.   }
  15. int main(){
  16.   int n=4;
  17.   cout<<TotalBSTPossible(n);
  18.   return 0;
  19.   }
Add Comment
Please, Sign In to add comment