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(唯一二叉搜索树)的更多相关文章

  1. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  2. [Leetcode] Unique binary search trees 唯一二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...

  4. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  5. [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 ...

  6. [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. [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 ...

  8. [leetcode]99. Recover Binary Search Tree恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. LeetCode OJ——Unique Binary Search Trees II

    http://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 一题要求得出所有树的种类数,二题要求得出所有树. 在一题的基础上修改代码, ...

  10. LeetCode OJ——Unique Binary Search Trees

    class Solution { public: int numTrees(int n) { ); vector<int> numVector; numVector.assign(n+,) ...

随机推荐

  1. LATEX教程(一)

    第一个文档 打开WinEdt,建立一个新文档,将以下内容复制进入文档中,保存,保存类型选择为UTF-8. \documentclass{article} \begin{document} hello, ...

  2. Java并发—线程池框架Executor总结(转载)

    为什么引入Executor线程池框架 new Thread()的缺点 每次new Thread()耗费性能 调用new Thread()创建的线程缺乏管理,被称为野线程,而且可以无限制创建,之间相互竞 ...

  3. oracle11g参数的简单查看方法

    1.查看processes和sessions参数show parameter processesshow parameter sessions 2.修改processes和sessions值alter ...

  4. 'is' in Python

    在Python中应该避免将“is”运算符用于比较 像“数值”和“字符串”这种不可变的值.由于Python内部操作这些对象的方式,使得对这些对象使用“is”运算符的结果将是不可预测的. 下面以两个例子加 ...

  5. 模块调用,datetime,time,logging,递归,双层装饰器, json,pickle迭代器和生成器

    一.python模块(导入,内置,自定义,开源) 1.模块简介 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用python ...

  6. SVN 过滤文件

    SVN新手最容易犯的一个错误: 就是把所有文件一股脑地全提交上去了. 这样很不好,因为这当中包含很多编译器自动生成的文件,还有中间文件. 这些文件可能每次编译都会不同,所以编译一次就冲突一次. 很显然 ...

  7. mybatis 一次执行多条SQL

    在默认情况下,一次性发过去的多条sql是不合法的. 想要让mysql一次执行多条sql语句,必须进行手动设置. 让mysql驱动开启批量执行sql的开关. 怎么开启呢?在拼装mysql链接的url时, ...

  8. maven junit.framework不存在问题解决

    问题 在使用maven进行一个工程的编译,已加入junit包的依赖,编译的时候却总是报“junit.framework不存在”错误. pom.xml中junit包加入如下: <dependenc ...

  9. 基于“基于dockerhub的jetty镜像的ossfs镜像”部署war包,遇到的文件夹读写权限被限制的问题解决方案

    前提: “基于dockerhub的jetty镜像的ossfs镜像” 已经搭建好了. 部署准备: 1.本地打包:war包-->idea工具 mvn 打包. 2.本地sh脚本:compile_vps ...

  10. android.intent.category.LAUNCHER和android.intent.action.MAIN

    一个应用程序可以有多个Activity,每个Activity是同级别的,那么在启动程序时,最先启动哪个Activity呢? 有些程序可能需要显示在程序列表里,有些不需要.怎么定义呢? android. ...