当前位置:网站首页>leetcode96不同的二叉搜索树
leetcode96不同的二叉搜索树
2022-07-01 23:54:00 【瀛台夜雪】
leetcode96:不同的二叉搜索树
题目描述
给你一个整数 n
,求恰由 n
个节点组成且节点值从 1
到 n
互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。
输入输出样例
输入:n = 3
输出:5
输入:n = 1
输出:1
算法一:使用动态规划
//二叉搜索树的定义:若其左子树不为空,则左子树上所有的结点的值均小于他的根结点,若他的右子树不空,则右子树上所有的节点的值均大于他的根节点的值
//其左右子树也为二叉查找树
int numTrees(int n)
{
//建立动态规划数组
vector<int>dp(n+1,0);
//设定初始条件
dp[0]=1;
dp[1]=1;
//设定转换的方程
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];
}
算法二:使用格兰数的定义
//卡塔兰数
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;
}
边栏推荐
- PyCharm调用matplotlib绘图时图像弹出问题怎么解决
- Algolia's search needs are almost closed
- Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
- How to display real-time 2D map after rviz is opened
- 股票开户哪个证券公司最好,有安全保障吗
- Why does blocprovider feel similar to provider?
- The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
- E-commerce RPA robot helps brand e-commerce to achieve high traffic
- 深度学习 | 三个概念:Epoch, Batch, Iteration
- UDS bootloader of s32kxxx bootloader
猜你喜欢
Kubernetes resource object introduction and common commands (III)
Material design component - use bottomsheet to show extended content (I)
[cmake] cmake configuration in QT Creator
【ES实战】ES上的安全性运行方式
kubernetes资源对象介绍及常用命令(三)
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
Is there a piece of code that makes you convinced by human wisdom
[QT] solve the problem that QT MSVC 2017 cannot compile
Niuke - Practice 101 - reasoning clown
【QT】QtCreator卸载与安装(非正常状态)
随机推荐
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
Is it safe to buy funds on Great Wall Securities?
华为HMS Core携手超图为三维GIS注入新动能
Using uni simple router, dynamically pass parameters typeerror: cannot convert undefined or null to object
[es practice] safe operation mode on ES
ADO. Net SqlDataAdapter object
ARP message header format and request flow
【QT】QtCreator卸载与安装(非正常状态)
RPA教程01:EXCEL自动化从入门到实操
北京炒股开户选择手机办理安全吗?
【C#】依赖注入及Autofac
【QT】對於Qt MSVC 2017無法編譯的問題解决
cookie、session、tooken
Relatively easy to understand PID understanding
2022年最佳智能家居开源系统:Alexa、Home Assistant、HomeKit生态系统介绍
字典、哈希表、数组的概念
使用htaccess文件禁止目录里的脚本执行权限
vs2015 AdminDeployment.xml
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
Three methods of finding inverse numbers