当前位置:网站首页>Leetcode96 different binary search trees
Leetcode96 different binary search trees
2022-07-02 00:00:00 【Yingtai night snow】
leetcode96: Different binary search trees
Title Description
Give you an integer n
, Seek just by n
Composed of nodes with node values from 1
To n
Different from each other Binary search tree How many kinds? ? Returns the number of binary search trees satisfying the meaning of the question .
I/o sample
Input :n = 3
Output :5
Input :n = 1
Output :1
Algorithm one : Use dynamic programming
// Definition of binary search tree : If its left subtree is not empty , Then the value of all nodes on the left subtree is less than its root node , If his right subtree is not empty , Then the value of all nodes on the right subtree is greater than the value of its root node
// Its left and right subtrees are also binary search trees
int numTrees(int n)
{
// Build dynamic programming array
vector<int>dp(n+1,0);
// Set initial conditions
dp[0]=1;
dp[1]=1;
// Set the equation of 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];
}
Algorithm 2 : Use the definition of the grand number
// Kataran number
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;
}
边栏推荐
- mysql之B tree 以及 B+tree
- ADO. Net SqlConnection object usage summary
- golang中的iota
- [QT] QT cannot find a solution to the compiler using msvc2017
- 正则表达式收集
- cookie、session、tooken
- Use vb Net to convert PNG pictures into icon type icon files
- 一个实习生的CnosDB之旅
- The difference between timer and scheduledthreadpoolexecutor
- ConcurrentSkipListMap——跳表原理
猜你喜欢
[es practice] safe operation mode on ES
PyCharm调用matplotlib绘图时图像弹出问题怎么解决
使用 pair 做 unordered_map 的键值
Key points of security agreement
The essence of software architecture
Use pair to do unordered_ Key value of map
Correlation - intra group correlation coefficient
Chapter 6 data flow modeling
【QT】QtCreator卸载与安装(非正常状态)
[QT] solve the problem that QT MSVC 2017 cannot compile
随机推荐
excel如何打开100万行以上的csv文件
Overview of edge calculation
微信小程序缓存过期时间的相关设置(推荐)
PyCharm调用matplotlib绘图时图像弹出问题怎么解决
Use pair to do unordered_ Key value of map
SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
【QT】对于Qt MSVC 2017无法编译的问题解决
Graduation season is both a farewell and a new beginning
Windows 7 install MySQL error: 1067
TS初次使用、ts类型
ADO. Net SqlCommand object
Using uni simple router, dynamically pass parameters typeerror: cannot convert undefined or null to object
USB-IF协会与各种接口的由来
【C#】依赖注入及Autofac
Review data desensitization system
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
2021 robocom world robot developer competition - preliminary competition of higher vocational group
Is it safe to choose mobile phone for stock trading account opening in Beijing?
攻防演练复盘
The difference between timer and scheduledthreadpoolexecutor