Given an integer n, the task is to find the total number of unique BSTs that can be made using values from 1 to n.
Examples:
Input: n = 3 Output: 5 Explanation: For n = 3, preorder traversal of Unique BSTs are: 1 2 3 1 3 2 2 1 3 3 1 2 3 2 1
Input: n = 2 Output: 2 Explanation: For n = 2, preorder traversal of Unique BSTs are: 1 2 2 1
Approach:
A binary search tree (BST) maintains the property that elements are arranged based on their relative order. Let’s define C(n) as the number of unique BSTs that can be constructed with n nodes.
When considering all possible BSTs, each of the n nodes can serve as the root. For any selected root, the remaining n-1 nodes must be divided into two groups:
Nodes with keys smaller than the root’s key
Nodes with keys larger than the root’s key.
Assume we choose the node with the i-th key as the root. In this scenario, there will be i-1 nodes that are smaller and n-i nodes that are larger than the chosen root. This leads to dividing the problem of counting the total BSTs with the i-th key as the root into two subproblems:
Calculating the number of unique BSTs for the left subtree containing i-1 nodes, represented as C(i-1).
Calculating the number of unique BSTs for the right subtree containing n-i nodes, represented as C(n-i).
Since the left and right subtrees are constructed independently, we multiply these counts to get the total number of BSTs for the current configuration: C(i-1) * C(n-i).
To find the total number of BSTs with n nodes, we sum this product across all possible roots (i.e., from i = 1 to n). Therefore,
C(n) = Σ(i = 1 to n) C(i-1) * C(n-i).
This formula corresponds to the recurrence relation for the nth Catalan number. So this is how it is a classic application of Catalan number. We just need to find nth catalan number. First few catalan numbers are 1 1 2 5 14 42 132 429 1430 4862, … (considered from 0th number).
// C++ code of finding Number of Unique// BST with N Keys #include<iostream>usingnamespacestd;// Function to calculate the binomial// coefficient C(n, k)intbinomialCoeff(intn,intk){// C(n, k) is the same as C(n, n-k)if(k>n-k){k=n-k;}intres=1;// Calculate the value of n! / (k! * (n-k)!)for(inti=0;i<k;++i){res*=(n-i);res/=(i+1);}returnres;}// Function to find the nth Catalan numberintnumTrees(intn){// Calculate C(2n, n)intc=binomialCoeff(2*n,n);// Return the nth Catalan numberreturnc/(n+1);}intmain(){intn=5;cout<<numTrees(n)<<endl;return0;}
Java
// Java program to find the number of unique// BSTs with N keysclassGfG{// Function to calculate the binomial// coefficient C(n, k)staticintbinomialCoeff(intn,intk){// C(n, k) is the same as C(n, n-k)if(k>n-k){k=n-k;}intres=1;// Calculate the value of n! / (k! * (n-k)!)for(inti=0;i<k;++i){res*=(n-i);res/=(i+1);}returnres;}// Function to find the nth Catalan numberstaticintnumTrees(intn){// Calculate C(2n, n)intc=binomialCoeff(2*n,n);// Return the nth Catalan numberreturnc/(n+1);}publicstaticvoidmain(String[]args){intn=5;System.out.println(numTrees(n));}}
Python
# Python program to find the number of unique # BSTs with N keys# Function to calculate the binomial coefficient C(n, k)defbinomial_coeff(n,k):# C(n, k) is the same as C(n, n-k)ifk>n-k:k=n-kres=1# Calculate the value of n! / (k! * (n-k)!)foriinrange(k):res*=(n-i)res//=(i+1)returnres# Function to find the nth Catalan numberdefnumTrees(n):# Calculate C(2n, n)c=binomial_coeff(2*n,n)# Return the nth Catalan numberreturnc//(n+1)n=5print(numTrees(n))
C#
// C# program to find the number of unique// BSTs with N keysusingSystem;classGfG{// Function to calculate the binomial// coefficient C(n, k)staticintBinomialCoeff(intn,intk){// C(n, k) is the same as C(n, n-k)if(k>n-k){k=n-k;}intres=1;// Calculate the value of n! / (k! * (n-k)!)for(inti=0;i<k;++i){res*=(n-i);res/=(i+1);}returnres;}// Function to find the nth Catalan// numberstaticintnumTrees(intn){// Calculate C(2n, n)intc=BinomialCoeff(2*n,n);// Return the nth Catalan numberreturnc/(n+1);}staticvoidMain(){intn=5;Console.WriteLine(numTrees(n));}}
JavaScript
// JavaScript program to find the number of unique BSTs with// n keys// Function to calculate the binomial coefficient C(n, k)functionbinomialCoeff(n,k){// C(n, k) is the same as C(n, n-k)if(k>n-k){k=n-k;}letres=1;// Calculate the value of n! / (k! * (n-k)!)for(leti=0;i<k;i++){res*=(n-i);res/=(i+1);}returnres;}// Function to find the nth Catalan numberfunctionnumTrees(n){// Calculate C(2n, n)letc=binomialCoeff(2*n,n);// Return the nth Catalan numberreturnc/(n+1);}constn=5;console.log(numTrees(n));
Output
42
Time Complexity: O(n), where n is nth catalan number. Auxiliary Space: O(1)