当前位置:网站首页>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;
}
边栏推荐
- LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
- Jielizhi, production line assembly link [chapter]
- [QT] qtcreator uninstall and installation (abnormal state)
- Pytorch learning record
- Graduation season is both a farewell and a new beginning
- vs2015 AdminDeployment.xml
- 北京炒股开户选择手机办理安全吗?
- [C #] dependency injection and Autofac
- Use the htaccess file to prohibit the script execution permission in the directory
- Openvino model performance evaluation tool DL workbench
猜你喜欢
Digital transformation has a long way to go, so how to take the key first step
TS初次使用、ts类型
LDR6035智能蓝牙音响可对手机设备持续充放电方案
起床困难综合症(按位贪心)
Use vb Net to convert PNG pictures into icon type icon files
Review data desensitization system
LeetCode中等题题分享(5)
[QT] QT cannot find a solution to the compiler using msvc2017
Difficult to get up syndrome (bit by bit greed)
mysql之B tree 以及 B+tree
随机推荐
leetcode96不同的二叉搜索树
学成在线案例实战
Selectively inhibiting learning bias for active sampling
SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
excel如何打开100万行以上的csv文件
mysql:insert ignore、insert和replace区别
Soft exam information system project manager_ Compiled abbreviations of the top ten management processes to help memory recitation - -- software test advanced information system project manager 054
ARP message header format and request flow
Linux CentOS7安装Oracle11g的超完美新手教程
golang中的iota
Deep learning | three concepts: epoch, batch, iteration
【ES实战】ES上的安全性运行方式
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
【C#】依赖注入及Autofac
Regular expression collection
[leetcode] length of the last word [58]
ADO. Net SqlDataAdapter object
cookie、session、tooken
Multi table operation - one to one, one to many and many to many
Openvino model performance evaluation tool DL workbench