当前位置:网站首页>牛客网:有多少个不同的二叉搜索树
牛客网:有多少个不同的二叉搜索树
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;
}
边栏推荐
- MySQL开放远程连接权限的两种方法
- “低代码”在企业数字化转型中扮演着什么角色?
- There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
- 猎头5万挖我去VC
- [download attached] installation and use of penetration test artifact Nessus
- Bidding announcement: remote disaster recovery project of Shenzhen Finance Bureau database
- 19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
- What is the difference between real-time rendering and pre rendering
- Distributed machine learning: model average Ma and elastic average easgd (pyspark)
- topic: Privacy, Deception and Device Abuse
猜你喜欢

ASP. Net core signalr tutorial

Policy Center > Google Play‘s Target API Level Policy

几百行代码实现一个 JSON 解析器

I 用c I 实现“栈”

Policy Center > Device and Network Abuse

技不压身,快速入门ETH智能合约开发,带你进入ETH世界

What is the difference between real-time rendering and pre rendering
Mysql8 error: error 1410 (42000): you are not allowed to create a user with grant solution

Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
Two methods for MySQL to open remote connection permission
随机推荐
Explain in detail the use of for loop, break and continue in go language
云技能提升好伙伴,亚马逊云师兄今天正式营业
今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
“低代码”在企业数字化转型中扮演着什么角色?
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
Two methods for MySQL to open remote connection permission
云和恩墨中标天津滨海农村商业银行2022-2023年度Oracle维保项目
How to connect the Internet Reading Notes - Summary
优惠券种类那么多,先区分清楚再薅羊毛!
Table responsive layout tips for super nice
Policy Center > Device and Network Abuse
Generating verification code with sring
分布式机器学习:模型平均MA与弹性平均EASGD(PySpark)
MySQL开放远程连接权限的两种方法
Build cloud native observability capability suitable for organizations
Cloud XR, how to help industrial upgrading
KDD 2022 | how far are we from the general pre training recommendation model? Universal sequence representation learning model unisrec for recommender system
Mysql事务/锁/日志总结
CVPR 2022丨特斯联AI提出:基于图采样深度度量学习的可泛化行人重识别
Create a new MySQL database under Linux and import SQL files