当前位置:网站首页>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;
}
边栏推荐
- S32Kxxx bootloader之UDS bootloader
- Use pair to do unordered_ Key value of map
- The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
- 使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
- ADO. Net SqlConnection object usage summary
- Write some suggestions to current and future doctoral students to sort out and share
- Concurrentskiplistmap -- principle of table skipping
- Difficult to get up syndrome (bit by bit greed)
- 【C#】依赖注入及Autofac
- [QT] QT cannot find a solution to the compiler using msvc2017
猜你喜欢
Openvino model performance evaluation tool DL workbench
Notblank and notempty
RPA教程01:EXCEL自动化从入门到实操
Shell process control
Material design component - use bottomsheet to show extended content (I)
Asp .NetCore 微信订阅号自动回复之文本篇
【QT】QtCreator卸载与安装(非正常状态)
Algolia's search needs are almost closed
Review data desensitization system
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
随机推荐
Chapter 6 data flow modeling
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Deep learning | three concepts: epoch, batch, iteration
E-commerce RPA robot helps brand e-commerce to achieve high traffic
Pytorch learning record
Is it safe to buy funds on Great Wall Securities?
cookie、session、tooken
Jielizhi Bluetooth headset quality control and production skills [chapter]
[embedded system course design] a single key controls the LED light
Key points of security agreement
Correlation - intra group correlation coefficient
2021 robocom world robot developer competition - preliminary competition of undergraduate group
启牛学院开户安全的吗?开户怎么开?
Algolia's search needs are almost closed
mysql:insert ignore、insert和replace区别
Jielizhi, production line assembly link [chapter]
Difficult to get up syndrome (bit by bit greed)
Three methods of finding inverse numbers
学成在线案例实战
JPA handwritten SQL, received with user-defined entity classes