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

解题:

单纯的看做一个数列题:1, 2, 5, 14, 42, 132, ... , 求通项。

这是一个典型的卡塔兰数,中国人明安图比卡塔兰早100多年就发现了这种数列,其实更应该叫明安图数。

程序实现不用这么做,不过还是给出通项公式:f(n) = (2n)! / [(n+1)! * n!];

在原列前补充一个值为1的0位:1, 1, 2, 5, 14, 42, 132, ...

则从新数列第3个数(2)开始,f(n) = f(0) * f(n-1)  +  f(1) * f(n-2)  +  ...  +  f(n-1) * f(0);

代码:

 class Solution {
public:
int numTrees(int n) {
if (n == )
return ;
vector<int> res(n+, );
res[] = res[] = ; for (int i = ; i <= n; ++i) {
for (int j = ; j < i; ++j) {
res[i] += res[j] * res[i--j];
}
} return res[n];
}
};

Leetcode测试集中没有考察n为0的情况,我认为n为0时还是有意义的,应该返回0。

												

【Leetcode】【Medium】Unique Binary Search Trees的更多相关文章

  1. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  2. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  3. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  4. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  5. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  6. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...

  7. 【LeetCode】96. Unique Binary Search Trees (2 solutions)

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  8. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

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

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

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

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

随机推荐

  1. 关于halo博客系统的使用踩坑——忘记登录密码

    踩坑: halo系统可以直接通过运行jar -jar halo-0.0.3.jar跑起来,也可以通过导入IDE然后运行Application的main方法跑起系统. h2数据库访问路径:http:// ...

  2. 再探display:table-cell &&左边固定、右边自适应

    display:table-cell;这个属性用的不多,它和td差不多,但是如果可以用好还是能收益不少的,下面举例说明. 1. 父元素display:table-cell,并设置verticle-al ...

  3. Python实现WEB QQ 登录与消息发送(第一版本 2015.06.26)

    WEB QQ的登录步骤与协议,需要的度娘下,很多. 转载说明来源:http://www.cnblogs.com/ryhan/p/4602762.html 我这实现是参考了度娘搜的 和自己抓包分析的. ...

  4. HDFS HA和Federaion

    1.HA HA即为High Availability,用于解决NameNode单点故障问题,该特性通过热备的方式为主NameNode提供一个备用者,一旦主NameNode出现故障,可以迅速切换至备Na ...

  5. 从外网GitHub clone开源项目的时候,.git文件过大,导致克隆慢

    以clone impala为例,主要是加入-depth=1参数: git clone -b cdh4-2.0 --depth=1 https://github.com/cloudera/Impala. ...

  6. iconfont_3种引用方式

    IconFont 图标的3种引用方式   新版Iconfont-阿里巴巴矢量图标库支持三种引用方式: 1.unicode引用(原始) unicode是字体在网页端最原始的应用方式,特点是: 兼容性最好 ...

  7. 二叉树链表C++实现

    结点的构造 源代码:https://github.com/cjy513203427/C_Program_Base/tree/master/57.%E4%BA%8C%E5%8F%89%E6%A0%91% ...

  8. SEO 小技巧汇总

    一.为了提高搜索点击率,还可以   1.为了增加关键词的密度,将关键字隐藏在页面里(将文字颜色定义成与背景颜色一样). 2.在图象的alt注释语句中加入关键字. 如:<IMG SRC=" ...

  9. 梯度寻优与logistic算法

    一.一些基本概念 最优化:在给定约束之下如何寻求某些因素(的量),以使某一(或某些)指标达到最优.高中学过的线性规划就是一类典型的最优化问题. 凸集:在集合空间中,凸集就是一个向四周凸起的图形.用数学 ...

  10. RegExp.prototype.exec()使用技巧

    RegExp.prototype.exec() exec() 方法在一个指定字符串中执行一个搜索匹配.返回一个结果数组或 null. 如果你只是为了判断是否匹配(true或 false),可以使用 R ...