ann8497

unique bt and bst samsung

Aug 17th, 2019
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int dp[100][100];
  5.  
  6.  
  7. /*
  8. For n = 0, 1, 2, 3, … values of Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …. So are numbers of Binary Search Trees.
  9.  
  10. here the concept of binomial coefficient is used that is
  11. C(n, k) = C(n-1, k-1) + C(n-1, k)
  12. C(n, 0) = C(n, n) = 1
  13. */
  14. int bst(int n, int k){
  15.    if(k ==0 || k ==n)return 1;
  16.    if(dp[n][k] != -1)return dp[n][k];
  17.     dp[n][k] =  bst(n-1,k-1) + bst(n-1,k);
  18.     return dp[n][k];
  19. }
  20.  
  21. // to calculate the binary trees just multiply n! to the number of bst's
  22. int main(){
  23.  
  24.    int n; cin>>n;
  25.    int a[n];
  26.    for(int i = 0; i<n; i++)cin>>a[i];
  27.    int A = 2*n;
  28.    int B = n;
  29.    for(int i = 0; i<100; i++)for(int j =0; j<100; j++)dp[i][j] = -1;
  30.    cout<<bst(A,B)/(n+1)<<endl;
  31.   return 0;
  32. }
Add Comment
Please, Sign In to add comment