当前位置:网站首页>leetcode96不同的二叉搜索树
leetcode96不同的二叉搜索树
2022-07-01 23:54:00 【瀛台夜雪】
leetcode96:不同的二叉搜索树
题目描述
给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。
输入输出样例

输入:n = 3
输出:5
输入:n = 1
输出:1
算法一:使用动态规划
//二叉搜索树的定义:若其左子树不为空,则左子树上所有的结点的值均小于他的根结点,若他的右子树不空,则右子树上所有的节点的值均大于他的根节点的值
//其左右子树也为二叉查找树
int numTrees(int n)
{
//建立动态规划数组
vector<int>dp(n+1,0);
//设定初始条件
dp[0]=1;
dp[1]=1;
//设定转换的方程
for(int i=2;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
dp[i]+=dp[j-1]*dp[i-j];
}
}
return dp[n];
}
算法二:使用格兰数的定义
//卡塔兰数
int numTrees2(int n)
{
long long c=1;
for(int i=0;i<n;i++)
{
c=c*2*(2*i+1)/(i+2);
}
return (int)c;
}
边栏推荐
- Create Ca and issue certificate through go language
- - Oui. Env. Fichier XXX, avec constante, mais non spécifié
- 图的遍历之深度优先搜索和广度优先搜索
- MySQL Replication中并行复制怎么实现
- 【CMake】Qt creator 里面的 cmake 配置
- Difficult to get up syndrome (bit by bit greed)
- Openvino model performance evaluation tool DL workbench
- 哈工大《信息内容安全》课程知识要点和难点
- [leetcode] length of the last word [58]
- The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
猜你喜欢

Redis AOF log

The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem

回顾数据脱敏系统

S32Kxxx bootloader之UDS bootloader
![[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler](/img/35/e458fd437a0bed4bace2d6d65c9ec8.png)
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler

Windows10 install WSL (I) (wslregisterdistribution error)

【必会】BM41 输出二叉树的右视图【中等+】

Shell process control

关联性——组内相关系数

Notblank and notempty
随机推荐
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
TS initial use, TS type
门级建模—课后习题
Depth first search and breadth first search of graph traversal
ADO.NET之SqlDataAdpter对象
notBlank 和 notEmpty
Key points and difficulties of the course "information content security" at Harbin Institute of Technology
在长城证券上买基金安全吗?
excel如何打开100万行以上的csv文件
MySQL: the difference between insert ignore, insert and replace
Use the htaccess file to prohibit the script execution permission in the directory
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
使用VB.net将PNG图片转成icon类型图标文件
Resumption of attack and defense drill
多表操作-一对一,一对多与多对多
TS初次使用、ts类型
ARP message header format and request flow
Chapter 6 data flow modeling
起床困难综合症(按位贪心)