题目:

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
Hide Similar Problems

(M) Unique Binary Search Trees II

 

链接: http://leetcode.com/problems/unique-binary-search-trees/

题解:

用DP求catalan number。  Cn+1 = ∑(Cn - i * Ci ), i 的范围是( 0 ~ n)。公式不太好写,改天要用Latex编辑一下。。有机会的话也要好好学习一下解析组合数学 - Analytic Combinatorics。Sedgewick有本书专门讲这个。

Time Complexity - O(n), Space Complexity - O(n)。

public class Solution {
public int numTrees(int n) { //catalan number
if(n <= 0)
return n;
int[] dp = new int[n + 1];
dp[0] = 1; for(int i = 1; i < dp.length; i++) {
for(int j = 0; j < i; j++) {
dp[i] += dp[(i - 1) - j] *dp[j];
}
} return dp[n];
}
}

或者用catalan数的另外一种推导,也是dp。

public class Solution {
public int numTrees(int n) { //Catalan number Cn+1 = 2(2n + 1)/ (n+2) * Cn
if(n < 0)
return 0; int[] count = new int[n + 1];
count[0] = 1; for(int i = 1; i < n + 1;i++)
count[i] = (int) (count[i - 1] * 2.0 *(2.0 *(i - 1) + 1.0) /(i - 1.0 + 2)); return count[n];
}
}

二刷:

求catalan number, 公式是Cn+1 = ∑(Cn - i * Ci ), 求和的范围是[0, n] 前后闭

Java:

public class Solution {
public int numTrees(int n) {
if (n <= 0) {
return n;
}
int[] dp = new int[n + 1];
dp[0] = 1;for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[(i - 1) - j] * dp[j];
}
}
return dp[n];
}
}

三刷:

这里要仔细注意一下dp数组的创建以及计算公式时的边界条件。 我们求第n个数的结果的话,其实是wiki公式里的第n + 1个数。所以我们建立一个长度为n + 1的一维数组dp,最后返回dp[n]就可以了。 其中Catalan number公式仍然用的是公式是Cn+1 = ∑(Cn - i * Ci ), 求和的范围是[0, n] 前后闭。所以假如我们要求dp[i], 那么内循环就是计算从0到 i-1 这 i 个数的乘积和。

Java:

Time Complexity - O(n), Space Complexity - O(n)。

public class Solution {
public int numTrees(int n) {
if (n <= 0) return 1;
int[] dp = new int[n + 1];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[j] * dp[(i - 1) - j];
}
}
return dp[n];
}
}

题外话:

有空的话还是要学一学离散数学的各种知识。

Reference:

http://www.codecogs.com/latex/eqneditor.php

http://en.wikipedia.org/wiki/Catalan_number

http://mathworld.wolfram.com/BinaryTree.html

96. Unique Binary Search Trees的更多相关文章

  1. 52. leetcode 96. Unique Binary Search Trees

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

  2. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

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

  3. 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]* ...

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

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

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

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

  6. 96. Unique Binary Search Trees (Tree; DP)

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

  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. 96. Unique Binary Search Trees(I 和 II)

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

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

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

随机推荐

  1. IIS6,IIS7 最简单的重写URL

    虽然现在很少用IIS6,今天突然要把项目搬到老的服务器上(IIS6),对项目还要重新部署一下. 主要把时间花在了对url的重写上.其实很简单,如下: IIS6 网站 → 属性 → 主目录 → 配置 → ...

  2. Eclipse中将项目导出jar包,以及转化成exe的方法

    Eclipse中将项目导出jar包,以及转化成exe的方法 http://wenku.baidu.com/view/385597f07c1cfad6195fa7c6.html

  3. WCF 宿主与通信模式(二)

    宿主 每个WCF服务都必须托管在Windows进程中,该进程称为宿主进程(host process) 单个宿主进程可以托管多个服务,相同的服务类型也可以托管在多个宿主进程中. wcf中托管服务一般有一 ...

  4. firefox ie chrome 设置单元格宽度 td width 有bug,不能正常工作。以下方式可以解决

    1. firefox ie chrome 设置单元格宽度 td width 有bug,不能正常工作. 如果是上面一行 和下面一行是分别属于两个table,但是他们的列需要对齐,也就是说分开画的,然后设 ...

  5. ES6学习笔记(十四)

    1.Promise的含义 Promise是异步编程的一种解决方案,比传统的解决方案--回调函数和事件--更合理和更强大.它由社区最早提出和实现,ES6将其写进了语言标准,统一了用法,原生提供了Prom ...

  6. PHP获取搜索引擎关键字来源(百度、谷歌、雅虎、搜狗、搜搜、必应、有道)

    <?php //获取来自搜索引擎入站时的关键词 function get_keyword($url,$kw_start) { $start=stripos($url,$kw_start); $u ...

  7. VB.Net 字符串加密类

    Public Class Cls_JM '使用 'Dim Jm As New Cls_JM(2) 'Dim strTmp As String 'Jm.jiemi(strTmp) 'Jm.Jiami(s ...

  8. Spark Streaming揭秘 Day23 启动关闭源码图解

    Spark Streaming揭秘 Day23 启动关闭源码图解 今天主要分析一下SparkStreaming的启动和关闭过程. 从Demo程序出发,主要聚焦在两段代码: 启动代码: 关闭代码: 启动 ...

  9. Linux进程间通信IPC学习笔记之消息队列(Posix)

    基础知识: 消息队列可认为是一个消息链表,有足够写权限的线程可往队列中放置消息,有足够读权限的线程可以从队列中取走消息.在某个进程往一人队列写入消息之前,并不需要另外某个进程在该队列上等待消息的到达. ...

  10. Xen学习——原理要点归纳总结

    Xen是半虚拟化,需要修改操作系统内核.Vmware是完全虚拟化. XEN的系统架构: Xen Hypervisor: 直接运行在硬件上,介于操作系统和硬件之间的一层软件,负责管理CPU.内存.中断. ...