leetcode: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
class Solution {
public:
int numTrees(int n) {
vector<int> num;
num.push_back(1); //在容器尾端插入一项数据,设置num[0]=1
for(int i=1; i<=n; i++){
num.push_back(0); //每次先将num[i]设置为0
if(i<3)
num[i]=i; //易知num[1]=1,num[2]=2
else{
for(int j=1; j<=i; j++)
num[i]+=num[j-1]*num[i-j]; //j为root节点,其左子树种数为j-1,右子树种数为i-j
}
}
return num[n];
}
};
其他解法:
1、1ms in C++ By Using Theorem From Graph Theory
This is my code. I use the property that the number of unique binary trees or n vertex is
{(2n)(2n-1)(2n-2)....(n+2)}/{(n)(n-1)....(2)(1)}
class Solution {
public:
int numTrees(int n) {
long long result = 1;
long long temp = 1;
for(int i = 2*n; i > n; i--){
result *= i;
temp *= (i-n);
if (result % temp == 0){
result /= temp;
temp = 1;
}
}
return result/(n+1);
}
};
2、2ms c++ using dp(动态规划)
class Solution {
public:
int numTrees(int n){
int arr[n+1][n+1];
memset(arr,0,sizeof(arr));
for(int len=1; len<=n; len++){
for(int j=1; j<=n-len+1; j++){
if(len == 1) arr[len][j] = 1;
else{
arr[len][j] += arr[len-1][j+1];
arr[len][j] += arr[len-1][j];
for(int k=1;k<len;k++) arr[len][j] += (arr[k][j]*arr[len-k-1][j+k+1]);
}
}
}
return arr[n][1];
}
};
3、
class Solution {
public:
int numTrees(int n) {
if(n==0) return 0;
int s[n+1];
int r;
s[0] = 1;
for(int i=1; i<n+1; i++)
{
s[i] = 0;
for(int l=0; l<i; l++)
{
r = i-1-l;
s[i] = s[i]+s[l]*s[r];
}
}
return s[n];
}
};
leetcode:Unique Binary Search Trees的更多相关文章
- [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 ...
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [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 && Unique Binary Search Trees II
1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
随机推荐
- 【BZOJ】【3856】Monster
又是一道水题…… 重点是分情况讨论: 首先我们很容易想到,如果a*k-b*(k+1)>0的话那么一定能磨死Monster. 但即使不满足这个条件,还有可能打死boss: 1.h-a<1也就 ...
- java中byte和blob互转
1. btye[]转blob byte[] bs = ... Blob blob = conn.createBlob(); blob.setBytes(1, bs); ps.setBlob(2, bl ...
- Codeforces Round #204 (Div. 2)->C. Jeff and Rounding
C. Jeff and Rounding time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Mac下使用Apache TCPMon
Mac下使用Apache TCPMon 参考链接: TCPMon Tutorial Anyone know how to get TCPMON working on a mac? Apache TCP ...
- HDU 4496 D-City(并查集,逆思维)
题目 熟能生巧...常做这类题,就不会忘记他的思路了... //可以反过来用并查集,还是逐个加边,但是反过来输出...我是白痴.....又没想到 //G++能过,C++却wa,这个也好奇怪呀... # ...
- foreach的参数不是数组:Warning: Invalid argument supplied for foreach
Warning: Invalid argument supplied for foreach() 问题Warning: Invalid argument supplied for foreach() ...
- hdu 4768 Flyer 二分
思路:由于最多只有一个是奇数,所以二分枚举这个点,每次判断这个点的左边区间段所有点的和作为 二分的依据. 代码如下: #include<iostream> #include<cstd ...
- github研究
一个程序猿一定会用git,但是我还没怎么用过,平时真是懒啊,学习之!...
- Java IO(四)
对象序列化 对象序列化又叫对象的持久化,对象的串行化(或反串行化) 当使用Serializable接口实现序列化操作时,如果一个对象中的某个属性不希望被序列化,则可以使用transient关键字进行声 ...
- 10个最佳的网站和App开发工具
这个世界充满了创新,开发的激情和决心是实现更高目标的关键因素.在网站开发中,毫无疑问,工具和可用的在线网页和 app 设计资源,发挥了重要的作用. 下面我们将带来一些网站和 app 的最佳工具. ...