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. 1 3 3 2 1
  2. \ / / / \ \
  3. 3 2 1 1 3 2
  4. / / \ \
  5. 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数目),这样只需双层循环即可,代码如下:
  1. class Solution {
  2. public:
  3. int numTrees(int n) {
  4. vector<int> num;
  5. num.push_back(1); //在容器尾端插入一项数据,设置num[0]=1
  6. for(int i=1; i<=n; i++){
  7. num.push_back(0); //每次先将num[i]设置为0
  8. if(i<3)
  9. num[i]=i; //易知num[1]=1,num[2]=2
  10. else{
  11. for(int j=1; j<=i; j++)
  12. num[i]+=num[j-1]*num[i-j]; //j为root节点,其左子树种数为j-1,右子树种数为i-j
  13. }
  14. }
  15. return num[n];
  16. }
  17. };

 其他解法:

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)}

  1. class Solution {
  2. public:
  3. int numTrees(int n) {
  4. long long result = 1;
  5. long long temp = 1;
  6. for(int i = 2*n; i > n; i--){
  7. result *= i;
  8. temp *= (i-n);
  9. if (result % temp == 0){
  10. result /= temp;
  11. temp = 1;
  12. }
  13. }
  14. return result/(n+1);
  15. }
  16. };

2、2ms c++ using dp(动态规划)

  1. class Solution {
  2. public:
  3. int numTrees(int n){
  4. int arr[n+1][n+1];
  5. memset(arr,0,sizeof(arr));
  6. for(int len=1; len<=n; len++){
  7. for(int j=1; j<=n-len+1; j++){
  8. if(len == 1) arr[len][j] = 1;
  9. else{
  10. arr[len][j] += arr[len-1][j+1];
  11. arr[len][j] += arr[len-1][j];
  12. for(int k=1;k<len;k++) arr[len][j] += (arr[k][j]*arr[len-k-1][j+k+1]);
  13. }
  14. }
  15. }
  16. return arr[n][1];
  17. }
  18. };

3、

  1. class Solution {
  2. public:
  3. int numTrees(int n) {
  4. if(n==0) return 0;
  5. int s[n+1];
  6. int r;
  7. s[0] = 1;
  8. for(int i=1; i<n+1; i++)
  9. {
  10. s[i] = 0;
  11. for(int l=0; l<i; l++)
  12. {
  13. r = i-1-l;
  14. s[i] = s[i]+s[l]*s[r];
  15. }
  16. }
  17. return s[n];
  18. }
  19. };

  

 

 

 

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. JSP页面动态联动

    效果如图: 页面用法: body部分: 注意:控制层Controller中:

  2. Hibernate联合主键映射

    1.联合主键的映射规则 1) 类中的每个主键属性都对应到数据表中的每个主键列. Hibernate要求具有联合主键的实体类实现Serializable接口,并且重写hashCode与equals方法, ...

  3. 【前端】js转码

    js转码 function urlencode (str) { str = (str + '').toString(); return encodeURIComponent(str).replace( ...

  4. JavaScript事件委托的技术原理

    如今的JavaScript技术界里最火热的一项技术应该是‘事件委托(event delegation)’了.使用事件委托技术能让你避免对特定的每个节点添加事件监听器:相反,事件监听器是被添加到它们的父 ...

  5. 偏序集的Dilworth定理

    定理1 令(X,≤)是一个有限偏序集,并令r是其最大链的大小.则X可以被划分成r个但不能再少的反链.其对偶定理称为Dilworth定理:定理2 令(X,≤)是一个有限偏序集,并令m是反链的最大的大小. ...

  6. Nagios 快速实现数据可视化的几种方式

    Nagios 是一款强大的开源监控软件,但他本身不能绘图,只能查看当前数据,不能看历史数据以及趋势,也正因此,想要更舒适的使用就要搭配绘图软件,现在可搭配的绘图软件有很多,例如 pnp4nagios, ...

  7. Jmeter测试Mysql

    一.在测试计划下,找到Add directory or jar to classpath下填入jdbc驱动路径. 二.新建线程组. 三.在线程组下,添加配置元件—JDBC Connection Con ...

  8. POJ 2823

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 35941   Accepted: 10636 ...

  9. 【hadoop】有参考价值的博客整理

    好文章的网址: hadoop shuffle机制中针对中间数据的排序过程详解(源代码级) Hadoop mapreduce原理学习 与 Hadoop 对比,如何看待 Spark 技术? 深入理解Had ...

  10. Linux的别名使用

    直接定义别名 编辑当前用户下的.bashrc 文件: vim  ~/.bashrc 添加别名为 lmysql 的命令语句 : alias lmysql='mysql -uroot -p -Dtest ...