给定正整数H ,任务是查找由第一(H + 1)个自然数组成的,高度为H的可能二进制搜索树的数量,并将其作为节点值。由于计数可能非常大,因此将其打印为10 9 + 7模。
例子:
Input: H = 2
Output: 4
Explanation: All possible BSTs of height 2 consisting of 3 nodes are as follows:
Therefore, the total number of BSTs possible is 4.
Input: H = 6
Output: 64
方法:可以根据以下观察结果解决给定问题:
- 只能使用(H + 1)个节点来形成高度为H的二叉树。
- 除根节点外,每个节点都有两种可能性,即成为左子节点还是右子节点。
- 将T(H)视为高度为H的BST数,其中T(0)= 1且T(H)= 2 * T(H – 1) 。
- 解决上述递归关系, T(H)的值为2 H。
因此,根据上述观察结果,将2 H的值打印为由第一(H +1)个自然数组成的高度H的BST总数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
const int mod = 1000000007;
// Function to calculate x^y
// modulo 1000000007 in O(log y)
int power(long long x, unsigned int y)
{
// Stores the value of x^y
int res = 1;
// Update x if it exceeds mod
x = x % mod;
// If x is divisible by mod
if (x == 0)
return 0;
while (y > 0) {
// If y is odd, then
// multiply x with result
if (y & 1)
res = (res * x) % mod;
// Divide y by 2
y = y >> 1;
// Update the value of x
x = (x * x) % mod;
}
// Return the value of x^y
return res;
}
// Function to count the number of
// of BSTs of height H consisting
// of (H + 1) nodes
int CountBST(int H)
{
return power(2, H);
}
// Driver Code
int main()
{
int H = 2;
cout << CountBST(H);
return 0;
}
Java
// Java program for the above approach
class GFG{
static int mod = 1000000007;
// Function to calculate x^y
// modulo 1000000007 in O(log y)
static long power(long x, int y)
{
// Stores the value of x^y
long res = 1;
// Update x if it exceeds mod
x = x % mod;
// If x is divisible by mod
if (x == 0)
return 0;
while (y > 0)
{
// If y is odd, then
// multiply x with result
if ((y & 1) == 1)
res = (res * x) % mod;
// Divide y by 2
y = y >> 1;
// Update the value of x
x = (x * x) % mod;
}
// Return the value of x^y
return res;
}
// Function to count the number of
// of BSTs of height H consisting
// of (H + 1) nodes
static long CountBST(int H)
{
return power(2, H);
}
// Driver code
public static void main(String[] args)
{
int H = 2;
System.out.print(CountBST(H));
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to calculate x^y
# modulo 1000000007 in O(log y)
def power(x, y):
mod = 1000000007
# Stores the value of x^y
res = 1
# Update x if it exceeds mod
x = x % mod
# If x is divisible by mod
if (x == 0):
return 0
while (y > 0):
# If y is odd, then
# multiply x with result
if (y & 1):
res = (res * x) % mod
# Divide y by 2
y = y >> 1
# Update the value of x
x = (x * x) % mod
# Return the value of x^y
return res
# Function to count the number of
# of BSTs of height H consisting
# of (H + 1) nodes
def CountBST(H):
return power(2, H)
# Driver Code
H = 2
print(CountBST(H))
# This code is contributed by rohitsingh07052
C#
// C# program for the above approach
using System;
class GFG{
static int mod = 1000000007;
// Function to calculate x^y
// modulo 1000000007 in O(log y)
static long power(long x, int y)
{
// Stores the value of x^y
long res = 1;
// Update x if it exceeds mod
x = x % mod;
// If x is divisible by mod
if (x == 0)
return 0;
while (y > 0)
{
// If y is odd, then
// multiply x with result
if ((y & 1) == 1)
res = (res * x) % mod;
// Divide y by 2
y = y >> 1;
// Update the value of x
x = (x * x) % mod;
}
// Return the value of x^y
return res;
}
// Function to count the number of
// of BSTs of height H consisting
// of (H + 1) nodes
static long CountBST(int H)
{
return power(2, H);
}
// Driver code
static void Main()
{
int H = 2;
Console.Write(CountBST(H));
}
}
// This code is contributed by abhinavjain194
输出:
4
时间复杂度: O(log 2 H)
辅助空间: O(1)