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

Summary: dynamic programming

     int numTrees(int n) {
vector<int> nums;
nums.push_back();
for(int i = ; i <= n; i ++) {
if(i == ){
nums.push_back();
}else if(i == ){
int num = i * nums[i - ];
nums.push_back(num);
}else if(i >= ){
int num = ;
for(int j = ; j <= i; j ++)
num += (nums[j - ] == ? : nums[j - ]) * (nums[i - j] == ? : nums[i - j]);
nums.push_back(num);
}
}
return nums[n];
}

Unique Binary Search Trees [LeetCode]的更多相关文章

  1. Unique Binary Search Trees leetcode java

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

  2. Unique Binary Search Trees——LeetCode

    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(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

  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] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  6. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  7. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  8. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  9. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

随机推荐

  1. Spring整合Tiles

    1.假设Spring相关的包和配置已经导入成功(后续有时间补上,本项目用的是3.2.0版本). 2.导入Tiles相关的jar包. tiles-api-2.2.2.jar tiles-core-2.2 ...

  2. poj 1502 最短路+坑爹题意

    链接:http://poj.org/problem?id=1502 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  3. FZU 2218 Simple String Problem(简单字符串问题)

    Description 题目描述 Recently, you have found your interest in string theory. Here is an interesting que ...

  4. 获取Token不完整问题

    有时会遇到获取Token只能获取一半的问题,明明有两个Cookie,但只获取到一个,这个是因为301重定向跳转设置问题,设置为True就可以获取到完整的Token了. myHttpWebRequest ...

  5. git学习笔记07-冲突了怎么办-那就解决冲突呗

    比如一个人自己创建了分支feature1进行修改提交之后提交,另一个人在master上修改然后提交. master分支和feature1分支各自都分别有新的提交,变成了这样: 这种情况下,Git无法执 ...

  6. jQuery 中的children()和 find() 的区别

    <!DOCTYPE html> <html> <head> <script type="text/javascript" src=&quo ...

  7. intanceof以及引出的__proto__和prototype

    instanceof运算代码 function instance_of(L, R) { //L 表示左表达式,R 表示右表达式 var O = R.prototype; // 取 R 的显示原型 L ...

  8. Mysql 命令行工具

    1.Mysql命令行工具分为两类:服务端命令行工具和客户端命令行工具. 2.服务端工具 mysql_install_db:建库工具 mysqld_safe:Mysql服务的启动工具,mysqld_sa ...

  9. java中String的常用方法

    java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...

  10. Spring Bean配置默认为单实例 pring Bean生命周期

    Bean默认的是单例的. 如果不想单例需要如下配置:<bean id="user" class="..." scope="singleton&q ...