Structurally Unique BST

PROBLEM :

Given an interger N, how many structurally unique binary search trees are there that store values 1...N?

For example, for N = 2, there are 2 unique BSTs

     1              2
      \            /
       2          1
For N = 3, there are 5 possible BSTs

  1              3        3         2      1
    \           /          /         /  \      \
     3        2         1         1   3      2
    /       /             \                        \
   2      1               2                       3

Input:
First line contains the test cases T.  1<=T<=15
Each test case have one line which is an interger N.  1<=N<=11

Example:
Input:
2
2
3

Output:
2
5
     
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int main()
 {
int t,no,i ;
long long num,den ;
cin>>t ;
while(t--)
{
   cin>>no ;
 
   //apply formula 2nCn/n+1
 
   num=1 ;
   for(i=no*2;i>no;i--)
       num=num*i ;
 
   den=1 ;
   for(i=1;i<=no;i++)
       den=den*i ;
     
   den=den*(no+1) ;
 
   cout<<num/den<<endl ;
}
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )