const int MOD = 1000000007;
long long totalTrees(int num){
vector<long long> dp(num + 1, 0);
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= num; ++i) {
for (int j = 1; j <= i; ++j) {
dp[i] = (dp[i] + dp[j-1] * dp[i-j]) % MOD;
}
}
return dp[num];
}
Problem of the day
You have been given a number ‘NUM’. Your task is to count total structurally unique binary search trees from keys 1 to ‘NUM’ considering we should use each key from 1 to ‘NUM’ only once.
Two binary search trees are different if there is at least one node during traversal which is different in values or present in one tree but not present in another tree.
Note:The answer can be large, hence the return answer % ‘MOD’, where ‘MOD’ is a large prime number (10^9 + 7).
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single integer ‘NUM’ where ‘NUM’ represents the number of keys.
Output Format :
For each test case, print a single line containing the count of total structurally unique binary search trees.
The output of each test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= NUM <= 500
Time limit: 1 sec
2
2
1
2
1
In the first test case, 2 unique BST are possible.
In the second test case, only 1 tree is possible.

2
3
4
5
14
In the first test case, 5 unique BST are possible.

In the second test case, similar to the above way, 14 unique BST are possible.
Can you solve the problem recursively?
We can assume a function for calculating the number of ways to calculate the number of trees for the given number of nodes for a fixed root. So we could calculate the number of ways to make a tree as follows. Let’s call the function F(‘X’) giving the number of trees given ‘X’ number of nodes.
We could calculate F(‘X’) = sum of { F(‘i’) * F(‘X’-1-’i’) }. Where i is the assumed number of nodes in the left child of the current root and so the number of remaining nodes for the right child is equal to (‘X’-1-i), hence we could add all these ways multiplicatively, so we could get the total number of ways.
This value of this function is also defined as Catalan number, which has a specified formula which is equal to ((2*NUM) C (NUM) / (NUM+1)) , but we will calculate the value recursively.
Steps for calculating assuming ‘NUM’ number of nodes in the given tree are as follows:
O(3 ^ NUM), where ‘NUM’ is the number of distinct keys.
The complexity estimation for above recursion can be done as follows:
Let's call the estimation function T(‘NUM’) for ‘NUM’ number of nodes , here it represents the number of recursive calls for ‘NUM’.
And hence time complexity will be O(3 ^ NUM).
O(3 ^ NUM), where ‘NUM’ is the number of distinct keys.
We can follow the same runtime complexity estimation as we are also creating the same amount of variables in the recursion stack memory and hence space complexity will be O(3 ^ NUM).
Interview problems
C++ Easy Solution
const int MOD = 1000000007;
long long totalTrees(int num){
vector<long long> dp(num + 1, 0);
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= num; ++i) {
for (int j = 1; j <= i; ++j) {
dp[i] = (dp[i] + dp[j-1] * dp[i-j]) % MOD;
}
}
return dp[num];
}
Interview problems
C++ DP Solution
long long int mod = 1e9+7;
long long solveRec(int n)
{
if(n<=1)
return 1;
long long ans = 0;
// think i as root node
for(int i=1; i<=n; i++) {
ans = ( ans + (solveRec(i-1) * solveRec(n-i))%mod )%mod;
}
return ans;
}
long long solveMem(int n, vector<long long>& dp)
{
if(n<=1)
return 1;
if(dp[n] != -1)
return dp[n];
long long ans = 0;
// think i as root node
for(int i=1; i<=n; i++) {
ans = ( ans + (solveMem(i-1, dp) * solveMem(n-i, dp))%mod )%mod;
}
return dp[n] = ans;
}
long long solveTab(int n)
{
vector<long long> dp(n+1, 0);
dp[0] = dp[1] = 1;
// i -> number of nodes
for(int i=2; i<=n; i++) {
// j-> root nodes
for(int j=1; j<=i; j++) {
dp[i] = ( dp[i] + (dp[j-1] * dp[i-j])%mod )%mod;
}
}
return dp[n];
}
long long totalTrees(int num)
{
// return solveRec(num);
// vector<long long> dp(num+1, -1);
// return solveMem(num, dp);
return solveTab(num);
}
Interview problems
c++
long long totalTrees(int n){
// Write your code here.
long long int mod=1e9+7;
vector<long long int> dp(n+1);
dp[0]=1;
for(int i=1;i<=n;i++)
{
long long int sum=0;
for(int j=0;j<i;j++)
{
sum+=(dp[j]*dp[i-j-1])%mod;
}
dp[i]=sum%mod;
}
return dp[n];
}
Interview problems
python solution
def totalTrees(n):
mod = 10**9 + 7
dp = [0] * (n + 1)
dp[0] = 1
for i in range(1, n + 1):
total_sum = 0
for j in range(i):
total_sum += (dp[j] * dp[i - j - 1]) % mod
dp[i] = total_sum % mod
return dp[n]Interview problems
c++ solution
long long totalTrees(int n){
// Write your code here.
long long int mod=1e9+7;
vector<long long int> dp(n+1);
dp[0]=1;
for(int i=1;i<=n;i++)
{
long long int sum=0;
for(int j=0;j<i;j++)
{
sum+=(dp[j]*dp[i-j-1])%mod;
}
dp[i]=sum%mod;
}
return dp[n];
}
Interview problems
Don't know if that is optimised or not
long long funct(int n,vector<long long>&dp){
if(n==0 || n==1)return 1;
if(n==2)return 2;
if(dp[n]!=-1)return dp[n];
int mod=1e9+7;
long long total=0;
for(int i=0;i<n/2;i++){
total=(total+(2*(funct(i,dp)*funct(n-1-i,dp))%mod)%mod)%mod;
}
if(n%2==1){
total=(total+(funct(n/2,dp)*funct(n/2,dp))%mod)%mod;
}
return dp[n]=total;
}
long long totalTrees(int num){
// Write your code here.
vector<long long>dp(num+1,-1);
return funct(num,dp);
}Interview problems
Discussion thread on Interview Problem | Count unique BSTs.
Hey everyone, creating this thread to discuss the interview problem - Count unique BSTs..
Practise this interview question on Coding Ninjas Studio (hyperlinked with the following link): Count unique BSTs.