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
解题: 有一个很简单的结论,就是节点数为n的二叉树共有h(n)种形态,其中h(n)是卡特兰数,h(n) = C(2*n,n)/(n+1);
所以只要按这个公式计算就是可以了。注意变量要取long long int型,否则会溢出。
代码:
 class Solution {
public:
int numTrees(int n) {
long long int n_factorial = ;
long long int tn_fac = ;
for(int i = ;i <= n;i ++)
n_factorial *= i;
for(int i = n+;i <= *n;i ++)
tn_fac *= i;
return (tn_fac)/(n_factorial*(n+));
}
};

题外话:为什么节点数为n的二叉树有h(n)种形态呢?

当n=0时,h(0)=1;

当n=1时,h(1)=1;

当n>=2时,分别考虑树的左子树和右子树,它们的节点数分别可以取(0,n-1),(1,n-2),....,(n-2,1),(n-1,0),所以h(n)=h(0)*h(n-1)+h(1)*h(n-2)+...+h(n-1,0)h(0),这个递推式解得到的数就是卡特兰数。

【leetcode刷题笔记】Unique Binary Search Trees的更多相关文章

  1. LeetCode(96) Unique Binary Search Trees

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

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

  3. LeetCode(95) Unique Binary Search Trees II

    题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...

  4. 【leetcode刷题笔记】Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  5. 【leetcode刷题笔记】Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  6. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  7. 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  8. 【leetcode刷题笔记】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  9. LeetCode(96)Unique Binary Search Trees

    题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...

  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. GB28181出内网

    最近关注GB28181的朋友很多,昨天有位朋友问到GB28181出内网的问题,希望我花5分钟的时间 讲讲如何通过GB28181协议将内网的摄像机视频推送到公网.要说清楚这个问题,5分钟的时间应该不 够 ...

  2. 安装npm install时,长时间停留在某一处的解决方案

    默认情况npm install安装时,会从 github.com 上下载文件,大部分安装不成功的原因都源自这里 因为 GitHub Releases 里的文件都托管在 s3.amazonaws.com ...

  3. CentOS4.5下LVS方案

    环境描述:本文在配置LVS时使用三台linux,一台做Directorserver (192.168.0.25) ,两台做realserver(192.168.0.127 192.168.0.12,在 ...

  4. HDU 4417 划分树+二分

    题意:有n个数.m个询问(l,r,k),问在区间[l,r] 有多少个数小于等于k. 划分树--查找区间第k大的数.... 利用划分树的性质.二分查找在区间[l,r]小于等于k的个数. 假设在区间第 i ...

  5. MySQL一:初识数据库

    阅读目录 一 数据库是什么 二 数据库的概念 三 MySQL介绍 四 下载安装 五 MySQL软件基本管理 一 数据库是什么 之前所学,数据要永久保存,比如用户注册的用户信息,都是保存于文件中,而文件 ...

  6. Linux下性能分析工具汇总

    来自:http://os.51cto.com/art/201104/253114.htm 本文讲述的是:CPU性能分析工具.Memory性能分析工具.I/O性能分析工具.Network性能分析工具. ...

  7. 已经mock类中引用的其它service类,但是在invoke私有方法的时候,该service类是空值

    错误原因:没有在开始测试用例的时候,初始化类的所有注解方法. 解决方法: 使用mock方法创建mock对象时,需要在测试用例执行前执行以下代码.通常, 这句代码可以放在测试基类或者@Before 中. ...

  8. 网络启动并安装Debian

    网络启动(PXEBoot)并安装Debian的官方文档在这里,不过官方文档有点冗长,我这里假设已经有一台安装好Debian,需要网络安装另一台(这台可以是虚拟机,通过ISO文件等等方式安装的).PXE ...

  9. linux模块导出符号 EXPORT_SYMBOL_GPL&EXPORT_SYMBOL(转)

    转自:http://blog.csdn.net/angle_birds/article/details/7396748 一个模块mod1中定义一个函数func1:在另外一个模块mod2中定义一个函数f ...

  10. ELK之jason配置nginx文件等多个配置文件

    [root@web02 ~]# cat /etc/logstash/conf.d/nginx.conf input { file { path => "/var/log/nginx/a ...