当前位置:网站首页>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;
}
边栏推荐
- Operate database transactions with jpatractionmanager
- The difference between timer and scheduledthreadpoolexecutor
- Difficult to get up syndrome (bit by bit greed)
- Door level modeling - after class exercises
- Asp .NetCore 微信订阅号自动回复之文本篇
- Various global files related to [.Net core] program
- Digital transformation has a long way to go, so how to take the key first step
- Use pair to do unordered_ Key value of map
- 【QT】Qt 使用MSVC2017找不到编译器的解决办法
- JPA handwritten SQL, received with user-defined entity classes
猜你喜欢

Shell process control

学成在线案例实战

2021 robocom world robot developer competition - preliminary competition of higher vocational group

The essence of software architecture

边缘计算概述

PostgreSQL source code (57) why is the performance gap so large in hot update?

Use vb Net to convert PNG pictures into icon type icon files

使用 pair 做 unordered_map 的键值

ARP message header format and request flow

Digital transformation has a long way to go, so how to take the key first step
随机推荐
Jielizhi, production line assembly link [chapter]
Correlation - intra group correlation coefficient
PyTorch学习记录
LeetCode中等题题分享(5)
Jielizhi, production line assembly link [chapter]
ERP项目施行计划的目的是什么?
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
回顾数据脱敏系统
【QT】对于Qt MSVC 2017无法编译的问题解决
Deep learning | three concepts: epoch, batch, iteration
SQL optimization
Openvino model performance evaluation tool DL workbench
[cmake] cmake configuration in QT Creator
Door level modeling - after class exercises
[embedded system course design] a single key controls the LED light
cookie、session、tooken
Chapter 6 data flow modeling
时间复杂度与空间复杂度
Algolia's search needs are almost closed
Operate database transactions with jpatractionmanager