LeetCode Unique Binary Search Trees (DP)
题意:
一棵BST有n个节点,每个节点的key刚好为1~n。问此树有多少种不同形态?
思路:
提示是动态规划。
考虑一颗有n个节点的BST和有n-1个节点的BST。从n-1到n只是增加了一个点n,那么点n可以放的地方并不多,而且有一些规律。由于n是最大的,所以必定是在最右边,但是它的上面和下面也可以有一些点,假设点n的上面有k个点,下面则为n-k-1个。观察到点n的上面的点必定是[1, n-k-1],下面的点是[n-k, n-1],而点数<n的BST的形态数都已经求出,那么枚举这个k值就行了。
class Solution {
public:
int numTrees(int n)
{
vector<int> que(,);
for(int i=; i<=n; i++)
{
int sum=;
for(int j=; j<i; j++)
sum+=que[j]*que[i-j-];
que.push_back(sum);
}
return que[n];
}
};
AC代码
LeetCode Unique Binary Search Trees (DP)的更多相关文章
- Unique Binary Search Trees(dp)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- 96. Unique Binary Search Trees(I 和 II)
Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 96. Unique Binary Search Trees (Tree; DP)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
随机推荐
- redis系列:通过日志案例学习string命令
前言 该文章将通过一个小demo将讲述Redis中的string类型命令.demo将以springboot为后台框架快速开发,iview前端框架进行简单的页面设计,为了方便就不使用DB存储数据了,直接 ...
- Glance代码分析
V2版本的glance-api采用Proxy的方式来构建对象(对象套对象),有点类似装饰器模式,包裹的顺序是 Auth(外层) -> Notifier -> Policy -> Qu ...
- 学习笔记:首次进行JUnit+Ant构建自动的单元测试(二)
关键字:JUnit,Ant,单元测试 终于把JUnit+Ant构建单元测试的大概了解了,其实我实践的过程是对了,只是指导博客(看到这里不懂请看我上一篇博客)本身的错误“成功”把我带入“坑”,有时候网友 ...
- 2017-10-26 NOIP模拟赛
三分咲 #include<iostream> #include<cstdio> #include<ctime> using namespace std; int n ...
- Spring MVC 参数的绑定方法
在Spring MVC中,常见的应用场景就是给请求的Url绑定参数.本篇就介绍两种最最基本的绑定参数的方式: 基于@RequestParam 这种方法一般用于在URL后使用?添加参数,比如: @Req ...
- python绘制世界人口地图
最近看了<python编程:从入门到实践>,里边设计的项目拿来学习学习,绘制世界人口地图. 首先,下载数据,http://data.okfn.org/ ,从这里下载population_d ...
- C++11随机数发生器
前言 一直知道所谓的"随机数"都是伪随机,事实上也是满足某种规则生成的.有些程序测试时通常需要一个随机数源,但在新标准出现之前,C++都是依赖简单的C库函数rand来生成随机数的. ...
- 【JavaScript权威指南】——逻辑与(&&)
三种用法总结: 1.布尔值计算: [成员]={false,true} 2.“真值”,“假值”计算: [假值]={false,null,undefined,0,-0,NaN,""} ...
- Duff and Meat(贪心)
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day ...
- 解决gap 采用increapment scn 方式 操作。
###########1 1.查看备库的scn ⚠️如果控制文件,数据文件,数据文件头部的scn不一致,需要根据日志中的gap的起始sequence# 找到对应的scn col current_sc ...