当前位置:网站首页>LeetCode_ 96_ Different binary search trees
LeetCode_ 96_ Different binary search trees
2022-07-28 18:45:00 【Fitz1318】
Topic link
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 .
Example 1:

Input :n = 3
Output :5
Example 2:
Input :n = 1
Output :1
Tips :
1 <= n <= 19
Their thinking
Dynamic programming
First, clarify the definition of binary search tree : It could be an empty tree , Or a binary tree that has the following properties : If its left subtree is not empty , Then the value of all nodes in the left subtree is less than the value of its root node ; If its right subtree is not empty , Then the value of all nodes in the right subtree is greater than the value of its root node ; Its left 、 The right subtree is also a binary sort tree .
By way of example 1 As an example ,dp[3] Is to use elements 1 Number of binary search trees for the root node + Elements 2 Is the number of root nodes + Elements 3 Is the number of root nodes . And according to the definition of binary search tree , It can be summarized as follows :
- 1 Is the number of root nodes = Zuo Zi Shu you 0 Number of elements * Right subtree has 2 Number of elements
- 2 Is the number of root nodes = Zuo Zi Shu you 1 Number of elements * Right subtree has 1 Number of elements
- 3 Is the number of root nodes = Zuo Zi Shu you 2 Number of elements * Right subtree has 0 Number of elements
- a key : Yes 2 The number of elements is
dp[2], Yes 1 The number of elements isdp[1]
So it can be concluded that dp[3] = dp[0] * dp[2] + dp[1] * dp[1] + dp[2] * dp[0], To sum up, we can get
dp[i] += dp[j-1] * dp[i-j]
among j Equivalent to the root node , from 1 Traversing i until .j-1 by j Is the number of left subtree nodes of the root node ,i-j Is the number of nodes in the right subtree
AC Code
class Solution {
public int numTrees(int n) {
if(n == 1){
return 1;
}
int[] dp = new int[n+1];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <=i ; j++) {
dp[i] += dp[j-1] * dp[i-j];
}
}
return dp[n];
}
}
边栏推荐
- DC simulation example of ADS simulation
- 实验楼----PHP大法
- 顿悟!百度强推的Redis天花板笔记,原来数据库是这样理解的
- Golang concurrency model
- 冒泡排序和相关视频
- Is it difficult for novices to change careers through self-study software testing?
- Summer Challenge [FFH] JS custom component: DIY a keyboard that can be used at any time! (I)
- GO exe生成图标版本信息
- Ue5 gas learning notes 1.7 task ability tasks
- What are the conditions for zero foundation learning software testing?
猜你喜欢

Introduction and advanced level of MySQL (6)

GC垃圾回收器详解

When golang encounters high concurrency seckill

MQTT over QUIC:下一代物联网标准协议为消息传输场景注入新动力

Noise of creative coding

USB type-C details

MYSQL入门与进阶(一)
![[GXYCTF2019]StrongestMind](/img/f4/61932548e0c7dd60d790d31fb5b96b.png)
[GXYCTF2019]StrongestMind

深圳线下报名|StarRocks on AWS:如何对实时数仓进行极速统一分析

NPM cannot recognize the "NPM" item as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is correct,
随机推荐
UE5 GAS 学习笔记 1.7 任务Ability Tasks
高德地图实现自定义小蓝点 自定义点标记 绘制多边形/圆形区域 根据地图的移动显示或者隐藏自定义点标记的相关实现
GO exe生成图标版本信息
深圳线下报名|StarRocks on AWS:如何对实时数仓进行极速统一分析
MongoDB数据库shell命令执行
Live broadcast starrocks technology insider: low base global dictionary optimization
First understanding of structure
MYSQL入门与进阶(一)
Tencent Tang Daosheng: open source is a new mode of production and collaboration in the era of industrial Internet
DC-DC switching power supply
Ue5 gas learning notes 1.2 game Tags
Multithreading and high concurrency -- source code analysis AQS principle
Meta Q2财报:营收首次下滑,Metaverse将与苹果竞争
1.3、链表
Introduction to main parameters of antenna
LVS manual
三分钟了解快来新媒体
Random talk on GIS data (VI) - projection coordinate system
MYSQL入门与进阶(六)
MYSQL入门与进阶(三)