当前位置:网站首页>96. Different binary search trees (medium binary search tree dynamic planning)
96. Different binary search trees (medium binary search tree dynamic planning)
2022-07-28 22:25:00 【Calm in the wind and rain】
96. Different binary search trees
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 .
Example 1:

Input :n = 3
Output :5
Example 2:
Input :n = 1
Output :1
Tips :
1 <= n <= 19
source : Power button (LeetCode)
link :https://leetcode.cn/problems/unique-binary-search-trees
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
Use dynamic planning , Find the state transition equation .
set up g(n) Express n Number of different binary search trees with nodes , set up f(i,n) In order to i Root 、 The sequence length is n The number of different binary search trees , So there are g(n) = f(1, n) + f(2, n) + … + f(n, n). The boundary conditions g(0) = 1, g(1) = 1, about f(i,n), It should be based on i Is the product of different left subtrees and different right subtrees of the root node , namely f(i,n) = g(i - 1) * g(n - i), that g(n) = f(1, n) + f(2, n) + … + f(n, n) Medium f All can be used g Instead of .
Answer key (Java)
class Solution {
public int numTrees(int n) {
int[] g = new int[n + 1];
g[0] = 1;
g[1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++) {
g[i] += g[j - 1] * g[i - j];
}
}
return g[n];
}
}
边栏推荐
- 【机器学习】朴素贝叶斯对文本分类--对人名国别分类
- The applet listens for the target node to appear on the screen
- CDN工作原理
- HCIP(10)
- Overall introduction of Ruiji takeout project
- Data visualization news, different forms of news reports
- HCIP(13)
- internet的基本服务中文件传输命令是哪个
- SQL注入 Less42(POST型堆叠注入)
- Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
猜你喜欢

SQL注入 Less34(POST型宽字节注入+布尔盲注)

(翻译)图技术简明历史

Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)

DHCP and PPPoE protocols and packet capture analysis

Have you seen the management area decoupling architecture? Can help customers solve big problems

Hcip seventh experiment

静态路由和缺省路由实验

Part 8: creating camera classes

MySQL built-in functions

Day3 classification management of Ruiji takeout project
随机推荐
MySQL built-in functions
Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)
拥抱开源指南
Part 8: creating camera classes
纪念一下第一次写的线段树了喽(对应洛谷3372)
静态成员static详解
HCIP(13)
TensorFlow Serving 高性能的机器学习模型服务系统
[Ruiji takeout] day05 package management business development
array_diff_assoc 元素是数组时不比较数组值的办法
Ruiji takeout - background login function development
Hcip experiment (14)
How to establish a decentralized community in Web3
【机器学习】朴素贝叶斯对文本分类--对人名国别分类
网易云信 2022Q2 产品补给站,快来获取你的产品补给计划吧!
HCIP(8)
hcip实验(14)
Learning notes and summary of C language programming specification
SQL注入 Less42(POST型堆叠注入)
gprs网络指的是什么