当前位置:网站首页>牛客网:有多少个不同的二叉搜索树
牛客网:有多少个不同的二叉搜索树
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;
}
边栏推荐
- Explain in detail the use of for loop, break and continue in go language
- dart:字符串replace相关的方法解决替换字符
- Openresty built in variable
- Cesium-1.72 learning (deploy offline resources)
- Imeta | Ye Mao / Shi Yu reviewed the dynamic shuttle and ecological function of intracellular and extracellular genes in the environmental microbiome
- Log4j2 进阶使用
- I 用c I 实现“栈”
- Arcmap操作系列:80平面转经纬度84
- Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
- [CVE-2019-0193] - Apache Solr DataImport 远程命令执行分析
猜你喜欢

Implementation of Devops in the core field of qunar, the Internet R & D Efficiency

I implement "stack" with C I

Create a new MySQL database under Linux and import SQL files

大学生研究生毕业找工作,该选择哪个方向?

什么是XR扩展现实,XR云串流平台有哪些
MySQL开放远程连接权限的两种方法

How cloudxr promotes the future development of XR

MySQL transaction / lock / log summary
![[algorithm] after summarizing the four linked lists, I brushed two interview questions](/img/04/1843e01cc91cdf10ae3d3d6a4f1e05.png)
[algorithm] after summarizing the four linked lists, I brushed two interview questions

Swagger's asp Net core web API help page
随机推荐
边缘计算平台如何助力物联网发展
Distributed machine learning: model average Ma and elastic average easgd (pyspark)
RTP 发送PS流零拷贝方案
How to get the preferential activities for stock account opening? Is online account opening safe?
招标公告:天津市住房公积金管理中心数据库一体机及数据库软件项目(预算645万)
Mysql代理中间件Atlas安装和配置
Phone number shielding function
I implement "stack" with C I
Parameter optimization - bias and variance
Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
[time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment
Open source STM32 USB-CAN project
互联网研发效能之去哪儿网(Qunar)核心领域DevOps落地实践
ASP. Net core Middleware
Interesting research on mouse pointer interaction
Types of waveguides
There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
技不压身,快速入门ETH智能合约开发,带你进入ETH世界
Three development trends of enterprise application viewed from the third technological revolution
云技能提升好伙伴,亚马逊云师兄今天正式营业