Total number of possible Binary Search Trees and Binary Trees with n keys (original) (raw)
Last Updated : 23 Jul, 2025
Given an **integer n, the task is to find the total number of **unique Binary Search trees And **unique Binary trees that can be made using values from **1 to n.
**Examples:
**Input: n = 3
**Output: BST = 5
BT = 30
**Explanation: _For n = 3, Total number of binary search tree will be 5 and total Binary tree will b 30.**Input: n = 5
**Output: BST = 42
BT = 5040
**Explanation: _For n = 5, Total number of binary search tree will be 42 and total Binary tree will b 5040.
**Approach:
1. **Unique Binary Search Trees (BSTs):
First let's see how we find Total number of **binary search tree with n nodes. _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
nnodes. This is given by the following recursive formula:
- **C(n) = Σ(i = 1 to n) C(i-1) * C(n-i).
This formula corresponds to the recurrence relation for the nth **Catalan number. Please refer to Number of Unique BST with N Keys for better understanding and proof. 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).
- Formula of catalan number is ****(1 / n+1) * (** **2*n Cn). Please refer to Applications of Catalan Numbers.
**2. Unique Binary Trees (General Binary Trees):
For general **binary trees, the nodes do not have to follow the Binary Search Tree property. The total number of unique binary trees is calculated as: Total Binary Trees = countBST(n) * n!
C++ `
// C++ code of finding Number of Unique // BST and BT with N Keys
#include using namespace std;
// Function to calculate the binomial // coefficient C(n, k) int binomialCoeff(int n, int k) {
// C(n, k) is the same as C(n, n-k)
if (k > n - k) {
k = n - k;
}
int res = 1;
// Calculate the value of n! / (k! * (n-k)!)
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;}
// Function to find the nth Catalan number int numBST(int n) {
// Calculate C(2n, n)
int c = binomialCoeff(2 * n, n);
// Return the nth Catalan number
return c / (n + 1);}
// Function to find total binary tree int numBT(int n) {
int fact = 1;
// Calculating factorial
for(int i = 1; i <= n; i++) {
fact = fact * i;
}
// Total binary tree = Tatal binary search tree * n!
return numBST(n) * fact;}
int main() {
int n = 5;
cout << numBST(n) << endl;
cout << numBT(n) << endl;
return 0;}
Java
// Java code of finding Number of Unique // BST and BT with N Keys class GfG {
// Function to calculate the binomial coefficient C(n, k)
static int binomialCoeff(int n, int k) {
// C(n, k) is the same as C(n, n-k)
if (k > n - k) {
k = n - k;
}
int res = 1;
// Calculate the value of n! / (k! * (n-k)!)
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
// Function to find the nth Catalan number
static int numBST(int n) {
// Calculate C(2n, n)
int c = binomialCoeff(2 * n, n);
// Return the nth Catalan number
return c / (n + 1);
}
// Function to find total binary tree
static int numBT(int n) {
int fact = 1;
// Calculating factorial
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
// Total binary tree = Total binary search tree * n!
return numBST(n) * fact;
}
public static void main(String[] args) {
int n = 5;
System.out.println(numBST(n));
System.out.println(numBT(n));
}}
Python
Python code of finding Number of Unique
BST and BT with N Keys
Function to calculate the binomial coefficient C(n, k)
def binomialCoeff(n, k):
# C(n, k) is the same as C(n, n-k)
if k > n - k:
k = n - k
res = 1
# Calculate the value of n! / (k! * (n-k)!)
for i in range(k):
res *= (n - i)
res //= (i + 1)
return resFunction to find the nth Catalan number
def numBST(n):
# Calculate C(2n, n)
c = binomialCoeff(2 * n, n)
# Return the nth Catalan number
return c // (n + 1)Function to find total binary tree
def numBT(n): fact = 1
# Calculating factorial
for i in range(1, n + 1):
fact *= i
# Total binary tree = Total binary search tree * n!
return numBST(n) * factn = 5 print(numBST(n)) print(numBT(n))
C#
// C# code of finding Number of Unique // BST and BT with N Keys using System;
class GfG {
// Function to calculate the binomial coefficient C(n, k)
static int binomialCoeff(int n, int k) {
// C(n, k) is the same as C(n, n-k)
if (k > n - k) {
k = n - k;
}
int res = 1;
// Calculate the value of n! / (k! * (n-k)!)
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
// Function to find the nth Catalan number
static int numBST(int n) {
// Calculate C(2n, n)
int c = binomialCoeff(2 * n, n);
// Return the nth Catalan number
return c / (n + 1);
}
// Function to find total binary tree
static int numBT(int n) {
int fact = 1;
// Calculating factorial
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
// Total binary tree = Total binary
// search tree * n!
return numBST(n) * fact;
}
static void Main() {
int n = 5;
Console.WriteLine(numBST(n));
Console.WriteLine(numBT(n));
}}
JavaScript
// JavaScript code of finding Number of Unique // BST and BT with N Keys
// Function to calculate the binomial coefficient C(n, k) function binomialCoeff(n, k) {
// C(n, k) is the same as C(n, n-k)
if (k > n - k) {
k = n - k;
}
let res = 1;
// Calculate the value of n! / (k! * (n-k)!)
for (let i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;}
// Function to find the nth Catalan number function numBST(n) {
// Calculate C(2n, n)
let c = binomialCoeff(2 * n, n);
// Return the nth Catalan number
return c / (n + 1);}
// Function to find total binary tree function numBT(n) { let fact = 1;
// Calculating factorial
for (let i = 1; i <= n; i++) {
fact *= i;
}
// Total binary tree = Total binary search tree * n!
return numBST(n) * fact;}
let n = 5; console.log(numBST(n)); console.log(numBT(n));
`
**Time Complexity: O(n), where **n is total number of node
**Auxiliary Space: O(1)