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
注意:二分查找树的定义是,左子树节点均小于root,右子树节点均大于root!不要想当然地将某个点作为root时,认为其他所有节点都能全部放在left/right中,除非这个点是 min 或者 max 的。
 
分析:本题其实关键是递推过程的分析,n个点中每个点都可以作为root,当 i 作为root时,小于 i  的点都只能放在其左子树中,大于 i 的点只能放在右子树中,此时只需求出左、右子树各有多少种,二者相乘即为以 i 作为root时BST的总数。
            开始时,我尝试用递归实现,但是超时了,可见系统对运行时间有要求。因为递归过程中存在大量的重复计算,从n一层层往下递归,故考虑类似于动态规划的思想,让底层的计算结果能够被重复利用,故用一个数组存储中间计算结果(即 1~n-1 对应的BST数目),这样只需双层循环即可,代码如下:
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的更多相关文章

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

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

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

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

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

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

  5. [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

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

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

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

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

  9. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

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

随机推荐

  1. 标准SQL

    1. SQL语句对大小写不敏感! 2. 查询和更新指令构成了 SQL 的 DML 部分: SELECT - 从数据库表中获取数据 UPDATE - 更新数据库表中的数据 DELETE - 从数据库表中 ...

  2. 【bzoj1010】[HNOI2008]玩具装箱toy

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9281  Solved: 3719[Submit][St ...

  3. 重定向 vs output redirect

    http://asawicki.info/files/visual_cpp_redirect.png http://asawicki.info/news_1496_redirecting_output ...

  4. shiro中unauthorizedUrl不起作用

    解决方法: 在shiro配置文件中添加(异常全路径做key,错误页面做value) <bean class="org.springframework.web.servlet.handl ...

  5. Xamarin 中开发Android实现全屏或者不显示标题栏的方法-宋兴柱

    using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; ...

  6. IT架构之IT架构模型——思维导图

    参考: [日] 野村综合研究所系统咨询事业本部. 图解CIO工作指南. 周自恒译 人民邮电出版社,2014

  7. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  8. iOS打电话,发短信,发邮件,打开网址

    //调用自带mail [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzl ...

  9. C#中的可空类型

    public class Person { public DateTime birth; public DateTime? death; string name; public TimeSpan Ag ...

  10. Razor视图引擎 语法学习(二)

    下面就和大家分享下我在asp.net官网看到的资料,学习到的点语法.1.通过使用@符号,可以直接在html页面中写C#或者VB代码:运行后: 2.页面中的C#或者VB代码都放在大括号中.运行后: 3. ...