Given a positive integer H , the task is to find the number of possible Binary Search Trees of height H consisting of the first (H + 1) natural numbers as the node values. Since the count can be very large, print it to modulo 109 + 7 .
Examples:
Input: H = 2Output: 4Explanation: All possible BSTs of height 2 consisting of 3 nodes are as follows:
Therefore, the total number of BSTs possible is 4.
Input: H = 6Output: 64
Approach: The given problem can be solved based on the following observations:
Only (H + 1) nodes are can be used to form a Binary Tree of height H . Except for the root node, every node has two possibilities, i.e. either to be the left child or to be the right child. Considering T(H) to be the number of BST of height H , where T(0) = 1 and T(H) = 2 * T(H – 1) . Solving the above recurrence relation , the value of T(H) is 2H .
Therefore, from the above observations, print the value of 2H as the total number of BST s of height H consisting of the first (H + 1) natural numbers .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int power(long long x, unsigned int y)
{
int res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
int CountBST(int H)
{
return power(2, H);
}
int main()
{
int H = 2;
cout << CountBST(H);
return 0;
}
Java
class GFG{
static int mod = 1000000007;
static long power(long x, int y)
{
long res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) == 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
static long CountBST(int H)
{
return power(2, H);
}
public static void main(String[] args)
{
int H = 2;
System.out.print(CountBST(H));
}
}
Python3
def power(x, y):
mod = 1000000007
res = 1
x = x % mod
if (x == 0):
return 0
while (y > 0):
if (y & 1):
res = (res * x) % mod
y = y >> 1
x = (x * x) % mod
return res
def CountBST(H):
return power(2, H)
H = 2
print(CountBST(H))
C#
using System;
class GFG{
static int mod = 1000000007;
static long power(long x, int y)
{
long res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) == 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
static long CountBST(int H)
{
return power(2, H);
}
static void Main()
{
int H = 2;
Console.Write(CountBST(H));
}
}
Javascript
<script>
var mod = 1000000007;
function power(x, y)
{
var res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
function CountBST(H)
{
return power(2, H);
}
var H = 2;
document.write( CountBST(H));
</script>
Time Complexity: O(log2 H)Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule. Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...