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+,) ...
随机推荐
- java中使用axis发布和调用webService及dom4j解析xml字符串
工作中需要调用webService服务,这里记录一下如何在java中发布和调用webService. 需要的jar包: webService服务端: import javax.jws.WebMetho ...
- MFC实现文字随鼠标移动
1 实验介绍 此实验是在刚开始接触MFC时做的.它要求实现的功能如下: 文字跟随鼠标动态移动(跟随移动方式自定) 修改图标为自己喜欢的图标 修改窗口标题 修改文档名称 可以用菜单项选定指定的颜色显示文 ...
- JavaScript实现自适应窗口大小的网页
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- 3.6中的range()
在python3中range()是这样的: >>> range(4) range(0, 4) #额,列表跑哪去了 在之前的python2中是这样的: >>> ran ...
- LeetCode:旋转图像【48】
LeetCode:旋转图像[48] 题目描述 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使 ...
- Informatica can bind a LONG value only for insert into a LONG column Oracle
Informatica实现etl同步表数据信息时 报: Severity Timestamp Node Thread Message Code Message ERROR 2016/8/8 17:32 ...
- 【LeetCode】【定制版排序】Sort Colors
之前转载过一篇STL的sort方法底层详解的博客:https://www.cnblogs.com/ygh1229/articles/9806398.html 但是我们在开发中会根据自己特定的应用,有新 ...
- octotree神器 For Github and GitLab 火狐插件
Code tree for GitHub and GitLabExtension to show code tree for GitHub and GitLab. Useful for develop ...
- python之json模块的基本使用
json模块的作用:将字符串和字典相互转换 json和eval的区别: eval函数不能识别null转换成None json可以将null转换成python可以识别的None json序列化和反序列化 ...
- JavaWeb CSS
1. CSS介绍 1.1. 什么是CSS CSS全称为Cascading Style Sheets,译为层叠样式表. 样式定义如何显示HTML元素. 样式通常存储在样式表中. 1.2. 百度百科 CS ...