当前位置:网站首页>牛客网:有多少个不同的二叉搜索树
牛客网:有多少个不同的二叉搜索树
2022-06-30 15:44:00 【lsgoose】
在力扣上说的很明白了,很有意思的推导:
首先,设F(i,n)表示n长度的序列中以i为根节点划分的二叉搜索树的个数,G(n)表示长度n的序列的二叉搜索树的个数,明显有:
而很明显,F(i, n)的个数其实是可以由两边子树的G求得的,如下所示:
然后就推导出了G的递推表达:
代码如下所示:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> G(n+1);
G[0]=G[1]=1;
for(int i=2;i<=n;++i){
for(int j=0;j<=i;++j){
G[i]+=G[j-1]*G[i-j];
}
}
cout<<G[n];
return 0;
}
边栏推荐
- Go-Micro安装
- Parameter optimization - bias and variance
- 详解Go语言中for循环,break和continue的使用
- 15年做糊21款硬件,谷歌到底栽在哪儿?
- 360 digital, ant group, etc. were selected as member units of the "business security promotion plan" of the Chinese Academy of Communications
- Siyuan notes: can you provide shortcut keys for folding all titles on the page?
- 大学生研究生毕业找工作,该选择哪个方向?
- Create statement for Oracle export view
- What is XR extended reality and what are the XR cloud streaming platforms
- 招标公告:2022年台州联通Oracle一体机和数据库维保服务项目
猜你喜欢
Modifying MySQL password under Linux: error 1396 (HY000): Operation alter user failed for 'root' @ 'localhost‘
备战数学建模34-BP神经网络预测2
Simulation of two-color ball system to judge the winning situation
Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
Cloud XR, how to help industrial upgrading
15年做糊21款硬件,谷歌到底栽在哪儿?
MySQL8.0开启远程连接权限的方法步骤
Map reduce case super detailed explanation
Cesium-1.72 learning (add points, lines, cubes, etc.)
CVPR 2022 - Tesla AI proposed: generalized pedestrian re recognition based on graph sampling depth metric learning
随机推荐
Smart wind power: operation and maintenance of digital twin 3D wind turbine intelligent equipment
MySQL8.0开启远程连接权限的方法步骤
Modifying MySQL password under Linux: error 1396 (HY000): Operation alter user failed for 'root' @ 'localhost‘
Bidding announcement: remote disaster recovery project of Shenzhen Finance Bureau database
Policy Center > Deceptive Behavior
[time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment
'&lt;', Hexadecimal value 0x3c, is an invalid problem solving
LeCun指明下一代AI方向:自主机器智能
分布式机器学习:模型平均MA与弹性平均EASGD(PySpark)
Implementation of Devops in the core field of qunar, the Internet R & D Efficiency
Mysql代理中间件Atlas安装和配置
Reptile (1) - Introduction to basic reptile theory
Open source STM32 USB-CAN project
招标公告:天津市住房公积金管理中心数据库一体机及数据库软件项目(预算645万)
Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
波导的种类
几百行代码实现一个 JSON 解析器
Interview experience of service end test engineer
The inspiration from infant cognitive learning may be the key to the next generation of unsupervised machine learning
Phone number shielding function