LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
一开始是想用递归解决,但看了标签是dp问题,就想了一下, 数目为k的bst,其每个 0 ~ k - 1都可以分成两个区间, 然后又可以生成bst, 所以k的bst种类数等于取k左侧与右侧可划分成bst的乘机的总和,额,有点绕口额,代码清晰一点, 看代码:
class Solution {
public:
int numTrees(int n) {
vector<int> ret;
if(n == ) return ;
ret.reserve(n + );
ret[] = ;
for(int i = ; i <= n; ++i){
if(i < ){
ret[i] = i;
continue;
}
for(int j = ; j < i; ++j){
ret[i] += ret[j] * ret[i - j - ];
}
}
return ret[n];
}
};
LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)的更多相关文章
- [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [Leetcode] Unique binary search trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [leetcode]98. Validate Binary Search Tree验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode OJ——Unique Binary Search Trees II
http://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 一题要求得出所有树的种类数,二题要求得出所有树. 在一题的基础上修改代码, ...
- LeetCode OJ——Unique Binary Search Trees
class Solution { public: int numTrees(int n) { ); vector<int> numVector; numVector.assign(n+,) ...
随机推荐
- Integrate-And-Fire Models(转)
Integrate-And-Fire Models 基础知识 轴突:动作电位(电位差形成电流)=神经递质发放=脉冲产生树突或细胞体:神经递质的接受=产生内外膜电位差(电流产生)=接收脉冲脉冲编码:多采 ...
- nginx基础系列
centos中搭建nginx环境 nginx开机启动 nginx配置文件说明 nginx负载均衡配置 nginx upstream模块 nginx配置ssl nginx日志切割 nginx平滑升级
- 曾经跳过的坑------JS中对象与结构体的声明和调用
直接上代码 正确的写法 //同一个ready方法中var viewModel = { // self.projectCode = PROJECT_CODE; BOOKEDCOUNT : 5, TOTA ...
- jelly
http://pwnny.cn/original/2016/06/26/MakeBlog.html#NativeBuild01 Jekyll和Github搭建个人静态博客
- 动手动脑:String.equals()的使用方法
public class StringEquals { /** * @param args the command line arguments */ public static void main( ...
- $《第一行代码:Android》读书笔记——第2章 Activity
(一)创建活动 1.创建活动类 创建没有Activity的项目,发现src文件夹是空的,手动创建一个包com.jyj.demo1,在包中添加一个名为MainActivity的class,该MainAc ...
- 020_自己编写的wordcount程序在hadoop上面运行,不使用插件hadoop-eclipse-plugin-1.2.1.jar
1.Eclipse中无插件运行MP程序 1)在Eclipse中编写MapReduce程序 2)打包成jar包 3)使用FTP工具,上传jar到hadoop 集群环境 4)运行 2.具体步骤 说明:该程 ...
- django大全
数据库配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME':'dbname', 'USER': 'ro ...
- Unity Json 之三
今天在网上看到一个simplejson,直接调用这两个API就可以了,简单易用 string jsonstr = SimpleJson.SimpleJson.SerializeObject(json) ...
- java基础之常量与变量
概要:通过这段时间的工作,发现自己的基础还是很薄弱的,so,you know 常量 一种特殊的变量,程序运行过程中不能改变的值 语法格式:final 数据类型 常量名称 = 常量值 例子:fina i ...