当前位置:网站首页>Leetcode 96 différents arbres de recherche binaires
Leetcode 96 différents arbres de recherche binaires
2022-07-02 00:00:00 【Neige nocturne à Yingtai】
leetcode96:Différents arbres de recherche binaires
Description du sujet
Pour vous donner un entier n
,Je vous en prie. n
Les noeuds sont composés et les valeurs des noeuds sont de 1
À n
Différents Arbre de recherche binaire Combien de?Renvoie le nombre d'arbres de recherche binaires qui répondent à l'intention du sujet.
Exemple d'entrée / sortie
Entrée:n = 3
Produits:5
Entrée:n = 1
Produits:1
Algorithme I:Utiliser la planification dynamique
//Définition de l'arbre de recherche binaire:Si son Sous - arbre gauche n'est pas vide,La valeur de tous les noeuds du sous - arbre gauche est inférieure à son noeud racine,Si son Sous - arbre droit n'est pas vide,La valeur de tous les noeuds du sous - arbre droit est supérieure à la valeur de son noeud racine
//Ses sous - arbres gauche et droit sont également des arbres de recherche binaires
int numTrees(int n)
{
//Créer un tableau de planification dynamique
vector<int>dp(n+1,0);
//Définir les conditions initiales
dp[0]=1;
dp[1]=1;
//Définir l'équation de conversion
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];
}
Algorithme 2:Utiliser la définition du nombre de glandes
//Nombre de catalans
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;
}
边栏推荐
- E-commerce RPA robot helps brand e-commerce to achieve high traffic
- Selectively inhibiting learning bias for active sampling
- excel如何打开100万行以上的csv文件
- [C #] dependency injection and Autofac
- .env.xxx 文件,加了常量,却undefined
- 2021 robocom world robot developer competition - preliminary competition of higher vocational group
- Digital transformation has a long way to go, so how to take the key first step
- Redis AOF log
- [QT] QT cannot find a solution to the compiler using msvc2017
- USB-IF协会与各种接口的由来
猜你喜欢
Door level modeling - after class exercises
Use pair to do unordered_ Key value of map
关联性——组内相关系数
algolia 搜索需求,做的快自闭了...
[QT] QT cannot find a solution to the compiler using msvc2017
Material design component - use bottomsheet to show extended content (I)
Jielizhi, production line assembly link [chapter]
写给当前及未来博士研究生一些建议整理分享
Is there a piece of code that makes you convinced by human wisdom
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
随机推荐
Concurrentskiplistmap -- principle of table skipping
vs2015 AdminDeployment.xml
How to realize parallel replication in MySQL replication
【.Net Core】程序相关各种全局文件
[C #] dependency injection and Autofac
Operate database transactions with jpatractionmanager
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
[QT] test whether QT can connect to the database
USB-IF协会与各种接口的由来
ADO. Net SqlConnection object usage summary
13 MySQL-约束
Use vb Net to convert PNG pictures into icon type icon files
启牛学院开户安全的吗?开户怎么开?
Selectively inhibiting learning bias for active sampling
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
记录一下大文件上传偶然成功偶然失败问题
MySQL Replication中并行复制怎么实现
TS initial use, TS type
使用VB.net将PNG图片转成icon类型图标文件
Graduation season is both a farewell and a new beginning