当前位置:网站首页>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];
}
}
边栏推荐
- Lin Xiaobin, head of Tencent cloud database, borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
- Is mov format a still image file format
- Which is the file transfer command in the basic services of the Internet
- SQL injection less42 (post stack injection)
- Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
- HYDAC overflow valve db08a-01-c-n-500v
- Summary of the use of hash table set and map when leetcode brushes questions
- Bugku,Web:都过滤了
- TensorFlow Serving 高性能的机器学习模型服务系统
- Sword finger offer II 055. Binary search tree iterator (medium binary search tree iterator)
猜你喜欢

HCIP(14)

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

Basic introduction of Rockwell AB PLC rslogix digital quantity IO module

HCIP(11)
![[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login](/img/53/c578e0d1428ea569fb412a20019924.png)
[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login

Win11怎么打开软件通知

HCIP第七次实验
![[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation](/img/3d/3def78ec88419712e14cf437e21447.png)
[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation

Netease Yunxin 2022q2 product supply station, come and get your product supply plan!

Embrace open source guidelines
随机推荐
Part 8: creating camera classes
Hcip seventh experiment
[CS231N]Lecture_ 2:Image Classification pipelin
How to establish a decentralized community in Web3
CDN工作原理
ssh 免密码登录
Ruiji takeout project - development of business development function Day2
Embrace open source guidelines
2021年数学建模B组代码
What does GPRS network mean
Record the fluent to solve the problem of a renderflex overflowed by 7.3 pixels on the bottom
array_ diff_ The method of not comparing array values when Assoc element is an array
Principle of object. Prototype. ToString. Call()
Bugku, Web: all filtered
Brief introduction to PCB materials
ECMASript 5/6 笔记
拥抱开源指南
深度学习必备:对数据集的拆分、根据拆分图片拆分labels、对全部标注标签进行区间检查
[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation
[Ruiji takeout] day05 package management business development