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. Leetcode#172 Fractorial Trailing Zero

    原题地址 n!含有多少个因子10,则结尾有多少个0 10=2*5,而2的个数肯定比5多,所以n!含有多少个因子5,则结尾有多少个0 如何计算n!有多少个因子5呢? 比如n=13,则: n! = 13 ...

  2. 4-Highcharts 3D图之3D普通饼图

    <!DOCTYPE> <html lang='en'> <head> <title>4-Highcharts 3D图之3D普通饼图</title& ...

  3. Codeforces Round #246 (Div. 2)——D题

    KMP算法,没写出来,完全不理解NEXT数组.现在理解了很多 答案都在程序中 ,不过这个思想真的很神奇, 还有毛语不好,一直没看懂题目,现在懂了, 大概是:S中前缀等于后缀,求其长度,和其在S中出现了 ...

  4. 用DIV+CSS切割多背景合并图片 CSS Sprites 技术

    很久之前就在互联网网站和一些js插件中见过这种技术的应用,当时觉得很麻烦,就没有用,也没有去深究. 近段时间一直在做前台的一些东西,涉及到很多div+css的问题.这个东东我又碰到了,所以我花了点时间 ...

  5. Kerberos的组件和术语(翻译和注解)

    之所以要翻译这篇文章,是因为提到了一些通常于对Kerberos协议简介性质的文章所没有提到的细节,而这些细节对于理解Kerberos的工作原理,以及Kerberos协议实现的使用都是很有必要的. 1. ...

  6. php中用于接受表单数据的$_request与$_post、$_get

    一.$_request与$_post.$_get的区别和特点 $_REQUEST[]具用$_POST[] $_GET[]的功能,但是$_REQUEST[]比较慢.通过post和get方法提交的所有数据 ...

  7. NGUI无法按住鼠标按住时无法监听OnHover事件

    UICamera.cs 修改前: if ((!isPressed) && highlightChanged) { currentScheme = ControlScheme.Mouse ...

  8. linux gcc loudong

    五事九思 (大连Linux主机维护) 大连linux维护qq群:287800525 首页 日志 相册 音乐 收藏 博友 关于我     日志       spcark_0.0.3_i386.src.t ...

  9. HTTP协议中的5类状态码

    ①   客户方错误      100   继续      101   交换协议     ②   成功      200    OK      201    已创建      202   接收      ...

  10. ACE 1.1.9 发布,开源云端代码编辑器

    点这里 ACE 1.1.9 发布,开源云端代码编辑器 oschina 发布于: 2015年04月06日 (1评) 分享到:    收藏 +25 4月18日 武汉 源创会开始报名,送华为开发板 ACE ...