96. Unique Binary Search Trees

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

思路:卡特兰数: c(0)=1, c(1)=1, c(2)=2, c(3)=5, c(4)=14…

卡特兰数的递推关系:

令h(0)=1, h(1)=1

h(n)=C(2n,n)/(n+1) =(2*n+1)!/(n+1)!*n! (n=0,1,2,...)

h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)*h(0) (n>=2)
例如:h(2)=h(0)*h(1)+h(1)*h(0)=1*1+1*1=2
        h(3)=h(0)*h(2)+h(1)*h(1)+h(2)*h(0)=1*2+1*1+2*1=5
以下代码利用的递推关系式:
h(n)=h(n-1)*(4*n-2)/(n+1);
注意:使用64位无符号整数(unsigned long long)最高可存储至第33个卡特兰数,再大的则只能用大数来存储了。
class Solution {
public:
int numTrees(int n)
{
unsigned long long catnums[];
if(n== || n==)
return ;
catnums[]=;
catnums[]=;
for(int i=;i<=n;i++)
catnums[i]=catnums[i-]*(*i-)/(i+);
return catnums[n];
}
};

其它卡特兰数的应用:

矩阵连乘:P = A1A2 ... An,一共有几种加括号的方案?h(n-1)

一个足够大的栈的进栈序列为1,2,3,⋯,n时有多少个不同的出栈序列?h(n)

凸多边三角形划分:在一个凸多边形中,通过若干条互不相交的对角线,把这个多边形划分成了若干三角形,给定凸多边形的边数n,求有多少种划分方案?h(n-2):例:4边形有2种划分方案,5边形有5种,6边形有14种。

求有n+1个叶结点的满二叉树的个数。

52. leetcode 96. 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 examp ...

  2. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

  3. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

  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 96 Unique Binary Search Trees ----- java

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

  6. Java [Leetcode 96]Unique Binary Search Trees

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

  7. [leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数

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

  8. [leetcode] 96 Unique Binary Search Trees (Medium)

    原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...

  9. [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 ...

随机推荐

  1. java中的vo、dto 、dao

    VO是跟数据库里表的映射,一个表对应一个VO  DAO是用VO来访问真实的表,对数据库的操作都在DAO中完成  BO是业务层,做逻辑处理的 VO , PO , BO , QO, DAO ,POJO  ...

  2. XAML: 获取元素的位置

    在之前讨论 ListView 滚动相关需求的文章中(UWP: ListView 中与滚动有关的两个需求的实现)曾经提到了获取元素相对位置的方法,即某元素相对另一元素的位置.现将所有相关方法再作整理,并 ...

  3. tcpdf导出pdf数据支持中文的解决方案

    步骤如下:1.确保你测试tcpdf能正常输出英文内容的pdf2.测试输入中文内容后显示是?的乱码或者空白分析原因,是因为我们输入的中文,tcpdf字体库并不支持,因此乱码或者空白显示 添加一个合适的字 ...

  4. 【Android Developers Training】 57. 在UI线程之外处理图像

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  5. JavaScript中的排序

    <script> //1. 冒泡排序 function bubbleSort(arr) { var len = arr.length; for (var i = 0; i < len ...

  6. .net MVC开源项目分享(1) 项目的基本情况

    介绍 本项目是mvcsolution框架的分支. 原项目地址:https://github.com/leotsai/mvcsolution/ 本项目地址:https://github.com/hewe ...

  7. touchmover手机移动端的拖动

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta name= ...

  8. c# ProxyServer 代理服务器 不是很稳定

    /**C# Programming Tips & Techniquesby Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill ...

  9. python - bilibili(四)抓包数据乱码

    上一篇文章中不知道大家发现端倪木有,两张照片对比很明显发现第一张是信息很明显的,第二张是乱码的. 为什么会出现这种情况?细心的童鞋可能发现是我们发送给服务器的请求连接的数据不同: 第一张图的信息是{& ...

  10. 集成 ssh第一阶段

    1.添加spring支持,包含spring-hibernate和spring-struts2.添加struts支持,包含struts-spring3.添加hibernate支持,在spring配置文件 ...