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. 解决重装 Oracle 出现的 INS-32025 问题,完全卸载 Oracle11g

    如果您要重装 Oracle,并且安装程序正在运行,请先关闭它. 完全卸载: 1.停止所有 Oracle 服务 2.通过开始菜单 Oracle Installation Products -> U ...

  2. Oracle PL/SQL编程之函数

    注: 以下测试案例所用的表均来自与scott方案,使用前,请确保该用户解锁. 代码的执行环境是在sqlplus中 1.简介 函数用于返回特定的数据,当建立函数时,函数头部必须包含return子句,而在 ...

  3. unity 读取外部exe程序控制台信息

    由于需要获取显卡信息,但是unity的自带函数,只能输出1个显卡 c#倒是可以但是引用了一个下载的dll   System.Management.dll 这个dll放到unity用不了,因为mono不 ...

  4. AtCoder Grand Contest 006 F - Blackout

    Description 在 \(n*n\) 的棋盘上给出 \(m\) 个黑点,若 \((x,y)\),\((y,z)\) 都是黑点,那么 \((z,x)\) 也会变成黑点,求最后黑点的数量 题面 So ...

  5. [转]weui-wxss WeUI for 小程序 为微信小程序量身设计

    本文转自:https://github.com/weui/weui-wxss/?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=tou ...

  6. 系统对象的使用——Cookie,ViewState,Session,Application

    Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable ...

  7. Expression Blend实例中文教程(9) - 行为快速入门Behaviors

    在Blend强大的设计功能支持下,设计人员和开发人员可以无代码实现Silverlight/WPF动画效果,例如上文介绍的StoryBoard,就是一个典型例子,设计人员和开发人员仅需提供必要元素,即可 ...

  8. 自动收缩数据库T-SQL

    alter database 数据库名 set auto_update_statistics off alter database 数据库名 set auto_update_statistics on

  9. 深入Java关键字null

    一.null是代表不确定的对象   Java中,null是一个关键字,用来标识一个不确定的对象.因此可以将null赋给引用类型变量,但不可以将null赋给基本类型变量.   比如:int a = nu ...

  10. spring security入门

    1. Restful API和传统API的区别 用URL描述资源 用http描述方法行为,用http状态码描述结果 使用json交互数据 RESTful是一种风格,不是强制的标准 2. 使用sprin ...