当前位置:网站首页>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;
}
边栏推荐
- The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
- 13 MySQL constraint
- Write some suggestions to current and future doctoral students to sort out and share
- 2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
- ADO. Net SqlCommand object
- JPA handwritten SQL, received with user-defined entity classes
- Digital transformation has a long way to go, so how to take the key first step
- 【QT】测试Qt是否能连接上数据库
- Relatively easy to understand PID understanding
- Windows 7 安装MYSQL 错误:1067
猜你喜欢
随机推荐
cookie、session、tooken
二叉搜索树的创建,查找,添加,删除操作
RPA教程01:EXCEL自动化从入门到实操
Concurrentskiplistmap -- principle of table skipping
kubernetes资源对象介绍及常用命令(三)
PyCharm调用matplotlib绘图时图像弹出问题怎么解决
PostgreSQL notes (10) dynamically execute syntax parsing process
ADO. Net SqlConnection object usage summary
.env.xxx 文件,加了常量,却undefined
TS初次使用、ts类型
Windows10 install WSL (I) (wslregisterdistribution error)
Write some suggestions to current and future doctoral students to sort out and share
【QT】Qt 使用MSVC2017找不到编译器的解决办法
2021 robocom world robot developer competition - preliminary competition of higher vocational group
[must] bm41 output the right view of the binary tree [medium +]
SQL optimization
学成在线案例实战
Key points and difficulties of the course "information content security" at Harbin Institute of Technology
求逆序数的三个方法
在长城证券上买基金安全吗?









