给定一个正整数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
Javascript
输出:
4
时间复杂度: O(log 2 H)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。