当前位置:网站首页>Leetcode skimming: dynamic programming 07 (different binary search trees)
Leetcode skimming: dynamic programming 07 (different binary search trees)
2022-07-25 19:16:00 【Taotao can't learn English】
96. Different binary search trees
Given an integer n, Seeking for 1 … n How many binary search trees are there for nodes ?
Example :

Didn't do it , The solution of the research problem was finally worked out . The idea is simple .
At present dp[ i ] The value of is that each node of all nodes in this tree is made a head node , The product of left subtree and right subtree changes is the sum , For example j Is the root node , More than j Left subtree composed of small numbers , Yes j - 1 Number , That is to say dp[ j - 1 ] Combinations of , Than j Large number , altogether i - j individual , Then the number of the right subtree has i - j individual , common dp [ i - j ] Change . Multiply left and right , For the current j Is the total change of nodes , All the total change is that each number is the head node , Sum of changes .
package com.programmercarl.dynamic;
/** * @ClassName NumTrees * @Descriotion TODO * @Author nitaotao * @Date 2022/7/25 17:00 * @Version 1.0 * https://leetcode.cn/problems/unique-binary-search-trees/ * 96. Different binary search trees **/
public class NumTrees {
public int numTrees(int n) {
/** * Binary search tree , Ascending * n>=1 */
if (n <= 2) {
return n;
}
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
// n>=1
for (int j = 1; j < i; j++) {
//dp[i] Each possibility is to take one of the numbers as the head node , Variation of left subtree * Variation of right subtree
// With dp[j] Is the head node
// Zuo Zi Shu you j-1 individual Right subtree has i-j individual
dp[i] += dp[j - 1] * dp[i - j];
}
}
return dp[n];
}
}

边栏推荐
- 基础乐理--配置和弦
- Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?
- 基于Mysql-Exporter监控Mysql
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
- [applet development] common components and basic usage details
- Care for front-line epidemic prevention workers, Haocheng JIAYE and Gaomidian sub district office jointly build the great wall of public welfare
- HTTP cache tongtianpian, there may be something you want
- Juzhi cloud computing opens a new era to the "proprietary cloud" of Youfu network
- Alibaba cloud free SSL certificate application detailed process
- MySQL sub query (selected 20 sub query exercises)
猜你喜欢

modelsim和quartus联合仿真PLL FIFO等IP核

【加密周报】加密市场有所回温?寒冬仍未解冻!盘点上周加密市场发生的重大事件!

Youth, oh, youth

Wechat campus maintenance and repair applet graduation design finished product (5) assignment of applet completion work

小程序毕设作品之微信校园维修报修小程序毕业设计成品(8)毕业设计论文模板

小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告

Dynamic implementation of wechat applet 27 progress bar and static construction of search box and hot search list

帝国CMS7.5仿《问答库》题库问答学习平台网站源码 带手机版

How to design product help center? The following points cannot be ignored

小程序毕设作品之微信校园维修报修小程序毕业设计成品(5)任务书
随机推荐
小程序毕设作品之微信校园维修报修小程序毕业设计成品(6)开题答辩PPT
QIIME2得到PICRUSt2结果后如何分析
常用的开发软件下载地址
SQL Server 2019 安装教程
Ping command details [easy to understand]
Talk about 11 tips for interface performance optimization
Pymoo学习 (8):Gradients
基于Mysql-Exporter监控Mysql
Everyone can participate in the official launch of open source activities. We sincerely invite you to experience!
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
有孚网络受邀参加2022全国CIO大会并荣获“CIO信赖品牌”称号
2022 IAA industry category development insight series report - phase II
[record of question brushing] 21. Merge two ordered linked lists
房地产行业大洗牌
Detailed explanation of Bluetooth protocol (what is Bluetooth)
leetcode刷题:动态规划07(不同的二叉搜索树)
Real estate industry reshuffle
Hongmeng - Damiao computing Sketchpad - Introduction
Pymoo学习 (7):并行化Parallelization
Common development software download addresses