当前位置:网站首页>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;
}
边栏推荐
- 股票开户哪个证券公司最好,有安全保障吗
- Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
- Write some suggestions to current and future doctoral students to sort out and share
- Notblank and notempty
- Key points and difficulties of the course "information content security" at Harbin Institute of Technology
- [leetcode] length of the last word [58]
- kubernetes资源对象介绍及常用命令(三)
- RPA tutorial 01: Excel automation from introduction to practice
- 时间复杂度与空间复杂度
- [cmake] cmake configuration in QT Creator
猜你喜欢

Learn online case practice

Door level modeling - after class exercises

边缘计算概述

How to solve the image pop-up problem when pycharm calls Matplotlib to draw

E-commerce RPA robot helps brand e-commerce to achieve high traffic

Redis RDB snapshot

.env.xxx 文件,加了常量,卻undefined

ARP message header format and request flow

Deep learning | three concepts: epoch, batch, iteration

mysql之B tree 以及 B+tree
随机推荐
Windows installation WSL (II)
13 MySQL-约束
RPA教程01:EXCEL自动化从入门到实操
The difference between timer and scheduledthreadpoolexecutor
2021 robocom world robot developer competition - preliminary competition of higher vocational group
[QT] qtcreator uninstall and installation (abnormal state)
【QT】Qt 使用MSVC2017找不到编译器的解决办法
Overview of edge calculation
微信小程序缓存过期时间的相关设置(推荐)
使用VB.net将PNG图片转成icon类型图标文件
JPA handwritten SQL, received with user-defined entity classes
ARP message header format and request flow
Is there a piece of code that makes you convinced by human wisdom
.env.xxx 文件,加了常量,卻undefined
ADO.NET 之sqlConnection 对象使用摘要
[embedded system course design] a single key controls the LED light
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
[cmake] cmake configuration in QT Creator
Record the accidental success and failure of uploading large files
起床困难综合症(按位贪心)