Unique BST's | Practice | GeeksforGeeks

Given an integer. Find how many structurally unique binary search trees are there that stores the values from 1 to that integer (inclusive). 
Example 1:
Input:
N = 2
Output: 2
Explanation:for N = 2, there are 2 unique
BSTs
     1               2  
      \            /
       2         1

Example 2:
Input:
N = 3
Output: 5
Explanation: for N = 3, there are 5
possible BSTs
  1           3     3       2     1
    \        /     /      /  \     \
     3      2     1      1    3     2
    /      /       \                 \
   2      1         2                 3
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function numTrees() which takes the integer N as input and returns the total number of Binary Search Trees possible with keys [1.....N] inclusive. Since the answer can be very large, return the answer modulo 10e9 + 7.
Expected Time Complexity: O(N2).
Expected Auxiliary Space: O(N).
Constraints:
1<=N<=1000
0
mohit negi3 days ago
dp : bottom up O(N*N)
idea :
let n=4
1 2 3 4
root=1 (L: 0, R: 3) no of bst(0) * noof bst(3)
+
root=2 (1,2)  bst(1)* bst(2);
+
....
sum( bst(i-1)*bst(n-i)) where i=1 to n;
 
int numTrees(int n)    {                int dp[n+1];       dp[0]=1;       dp[1]=1;              for(int i=2;i<=n;i++)       {   long long sum=0;           for(int j=1;j<=i;j++)           {                  sum=((sum%mod)+((dp[j-1]%mod)*(dp[i-j]%mod))%mod)%mod;           }           dp[i]=sum%mod;       }       return dp[n]%mod;   }
0
ambikagarg11011 week ago
Using Memoization
 
long long dp[1001];   long long MOD = 1e9+7;   long long solve(int N)   {      if(dp[N]!=-1)      {          return dp[N];      }      if(N==0 or N==1)      {          return dp[N] = 1;      }      else      {          long long sum=0;          for(int i=1;i<=N;i++){              sum = (sum%MOD + ((solve(i-1)%MOD)*(solve(N-i)%MOD))%MOD)%MOD;          }          return dp[N] = sum;      }   }   int numTrees(int N)    {     memset(dp,-1,sizeof(dp));     return (int)solve(N);   }
0
vinamrajha1 week ago
int numTrees(int N) 
   {
       // Your code here
       // same as catalan number series
       long long d =1e9+7;
       int dp[N+1];
       dp[0] =1;
       for(int i =1; i<=N; ++i){
           dp[i] =0;
           for(int j =0; j<i; ++j)dp[i] =(dp[i]%d+(dp[j]%d*dp[i-j-1]%d))%d;
       }
       return dp[N];
   }
0
shivam1706203 weeks ago
 //Function to return the total number of possible unique BST.    long long mod=1e9+7;   int numTrees(int n)    {       // Your code here       vector<int> dp(n+1 , 0);              // catalan number(n) = summation i=0 , n-1 ( c(i)*c(n-i-1))       // c(n)=c(0)*c(n-1) + c(1)*c(n-2) + c(2)*c(n-3) + ... + c(n-2)*c(1) + c(n-1)*c(0)              dp[0]=1;// base case;              for(int i=1 ;i<=n;i++)       {           for(int j=0;j<i;j++)           {               dp[i]=(dp[i]%mod + (dp[j]%mod * dp[i-j-1]%mod)%mod)%mod;           }       }              return dp[n];   }
0
yugi20003 weeks ago
Complete Java Solution:
 
    static int numTrees(int N)
    {
        // Your code goes here
        long[] dp = new long[N+1];
        int mod = 1000000007;
        dp[0] = 1;
        
        for(int i = 1; i < N+1; ++i) {
            long bsts = 0;
            for(int low = 0, high = i-1; low < i; ++low, --high) {
                bsts += (dp[low]%mod * dp[high]%mod)%mod;
            }
            dp[i] = bsts%mod;
        }
        
        return (int)dp[N];
    }
    
0
devashishbakare3 weeks ago
Get  better understanding of this cattlon number explanation
0
rohitjug19csPremium1 month ago
Just needed to find nth catalan number :
static int numTrees(int n)
    {
       int mod = 1000000007; 
       long dp[] = new long[n+1];
       dp[0] = 1;
       dp[1] = 1;
       
       for(int i=2; i<n+1; i++){
           for(int j=0, k=i-1; j<i && k>=0; j++, k--)
               dp[i] = (dp[i] + (dp[j]*dp[k])%mod)%mod;
               
           
       }
       return (int)dp[n];
        
    }
0
vishalpandey100220002 months ago
 static int mod = 1000000007;   static int numTrees(int N)   {       // Your code goes here       long dp[]=new long[N+1];       dp[0]=1;       for (int i=1;i<=N;i++){           dp[i]=0;           for (int j=0;j<i;j++){               dp[i]=(dp[i]+dp[j]*dp[i-j-1])%mod;           }       }       return (int)dp[N];                 }
0
lindan1232 months ago
public:
    //Function to return the total number of possible unique BST. 
    int numTrees(int n) 
    {
         long long dp[n+1];
        int sum=0;
        dp[0]=1;
        dp[1]=1;
        long long mod = 1000000007;
        for(int i=2;i<=n;i++)
        {
            sum=0;
            for(int j=1;j<=i;j++)
            {
                sum = (sum+(dp[i-j]*dp[j-1])%mod)%mod;
            }
            dp[i] = sum;
        }
        
        
        return dp[n];
    }
Time Taken : 0.1
Cpp
+1
onibabahaha1234565 months ago
Just use nth catalan number technique :)
 int numTrees(int N) 
    {
        // Your code here
        long long mod=1000000007;
        long long dp[N+1];
        dp[0]=1;
        dp[1]=1;
        for (int i=2;i<N+1;i++){
            long long sum=0;
            for (int j=0, k=i-1;j<i,k>=0;j++,k--){
                sum=(sum+(dp[j]*dp[k])%mod)%mod;
            }
            dp[i]=sum;
        }
        return dp[N]%mod;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.