N 个节点的高度至少为 K 的 BST 计数
给定两个正整数N和K ,任务是找到高度大于或等于K的N个节点的二叉搜索树(BST) 的数量。
注意:这里的高度是指 BST 的最大深度。
例子:
Input: N = 3, K = 3
Output: 4
Explanation:
There are 4 possible binary search trees with height greater than or equal to K.
1 1 3 3
\ \ / /
2 3 2 1
\ | / \
3 2 1 2
Input: N = 4, K = 2
Output: 14
方法:这个问题可以用递归来解决。请按照以下步骤解决问题:
- 基本情况:如果N等于1则返回1 。
- 将变量countBsts初始化为0以存储有效 BST 的数量。
- 使用变量i在[1, N]范围内迭代:
- 如果i-1且Ni大于等于K ,则:
- 递归调用左子树的函数,节点为i-1 ,高度为 K-1 ,它将找到创建左子树的方法数。
- 递归调用右子树的函数,节点为N-i ,高度为K-1 ,它将找到创建右子树的方法数。
- 将结果添加到countBsts。
- 如果i-1且Ni大于等于K ,则:
- 完成上述步骤后,打印countBsts。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of BST's
// with N nodes of height greater than
// or equal to K
int numOfBst(int n, int k)
{
if (n <= 1) {
return 1;
}
// Store number of valid BSTs
int countBsts = 0;
// Traverse from 1 to n
for (int i = 1; i <= n; i++) {
// If Binary Tree with height greater
// than K exist
if (i - 1 >= k or n - i >= k)
countBsts
+= numOfBst(i - 1, k - 1)
* numOfBst(n - i, k - 1);
}
// Finally, return the countBsts
return countBsts;
}
// Driver Code
int main()
{
// Given Input
int n = 4;
int k = 2;
// Function call to find the number
// of BSTs greater than k
cout << numOfBst(n, k - 1) << endl;
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find the number of BST's
// with N nodes of height greater than
// or equal to K
static int numOfBst(int n, int k)
{
if (n <= 1) {
return 1;
}
// Store number of valid BSTs
int countBsts = 0;
// Traverse from 1 to n
for (int i = 1; i <= n; i++)
{
// If Binary Tree with height greater
// than K exist
if (i - 1 >= k || n - i >= k)
countBsts += numOfBst(i - 1, k - 1)
* numOfBst(n - i, k - 1);
}
// Finally, return the countBsts
return countBsts;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int n = 4;
int k = 2;
// Function call to find the number
// of BSTs greater than k
System.out.println(numOfBst(n, k - 1));
}
}
// This code is contributed by potta lokesh.
Python3
# python 3 program for the above approach
# Function to find the number of BST's
# with N nodes of height greater than
# or equal to K
def numOfBst(n, k):
if (n <= 1):
return 1
# Store number of valid BSTs
countBsts = 0
# Traverse from 1 to n
for i in range(1, n + 1, 1):
# If Binary Tree with height greater
# than K exist
if (i - 1 >= k or n - i >= k):
countBsts += numOfBst(i - 1, k - 1) * numOfBst(n - i, k - 1)
# Finally, return the countBsts
return countBsts
# Driver Code
if __name__ == '__main__':
# Given Input
n = 4
k = 2
# Function call to find the number
# of BSTs greater than k
print(numOfBst(n, k - 1))
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the number of BST's
// with N nodes of height greater than
// or equal to K
static int numOfBst(int n, int k)
{
if (n <= 1) {
return 1;
}
// Store number of valid BSTs
int countBsts = 0;
// Traverse from 1 to n
for (int i = 1; i <= n; i++)
{
// If Binary Tree with height greater
// than K exist
if (i - 1 >= k || n - i >= k)
countBsts += numOfBst(i - 1, k - 1)
* numOfBst(n - i, k - 1);
}
// Finally, return the countBsts
return countBsts;
}
// Driver code
static void Main()
{
// Given Input
int n = 4;
int k = 2;
// Function call to find the number
// of BSTs greater than k
Console.WriteLine(numOfBst(n, k - 1));
}
}
// This code is contributed by divyeshrabadiya07.
Javascript
14
时间复杂度: O(2 n )
辅助空间: O(1)