Unique Binary Search Trees [LeetCode]
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]的更多相关文章
- Unique Binary Search Trees leetcode java
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- Unique Binary Search Trees——LeetCode
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- [LeetCode] 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 II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- 【一天一道LeetCode】#96. Unique Binary Search Trees
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- [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 ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
随机推荐
- SSMS Tools Pack
http://www.ssmstoolspack.com/Features Format SQL 大小写切换,只切换关键字 http://www.ssmstoolspack.com/Features? ...
- 指令随笔之:tail、cat、scp、&、&&、;、|、>、>>
tail(中文意思是跟踪) tail默认只看文件的最后10行内容,cat则一次显示全部内容 ping 192.168.120.204 > zyx.log & # &表 ...
- MemSQL Start[c]UP 2.0 - Round 1(无聊练手B题)
http://codeforces.com/contest/452/problem/B B. 4-point polyline time limit per test 2 seconds memo ...
- win8下安装matlab7.0
在win8下安装matlab7.0会出现一些兼容性的问题,需要设置系统环境变量,修改方式如下. 1.设置环境变量,方法:在你的安装目录的\MATLAB7\bin\win32有一个叫做atlas_Ath ...
- SynchronizationContext的研究之一(非WPF及Forms)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Nemerle Quick Guide
This is a quick guide covering nearly all of Nemerle's features. It should be especially useful to a ...
- SAP接口编程 之 JCo3.0系列(04) : 会话管理
在SAP接口编程之 NCo3.0系列(06) : 会话管理 这篇文章中,对会话管理的相关知识点已经说得很详细了,请参考.现在用JCo3.0来实现. 1. JCoContext 如果SAP中多个函数需要 ...
- http1.1和http1.0区别
http1.1和服务器建立连接可以获得多个资源 http1.0和服务器建立连接可以获取一个值
- (二)shell中case语句、程序传参、while
2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...
- Object Pascal 过程与函数
过程与函数 过程与函数是实现一定功能的语句块,是程序中的特定功能单元.可以在程序的其他地方被调用,也可以进行递归调用.过程与函数的区别在于过程没有返回值,而函数有返回值. 1.过程与函数的定义 过程与 ...