题目:

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. java web中cookie的永久创建与撤销

    一.首先是创建cookie 当在数据库中查找知道所输入的用户名和密码正确之后,就开始创建: String cb=request.getParameter("cb");//cb就是登 ...

  2. Adobe Illustrator CS6 绿色简体中文版下载地址

    一.Adobe Illustrator CS6 简体中文精简绿色优化版:1.由官方简体中文正式版制作而成,只需要执行一次快速安装即可使用.已经注册,非tryout版,支持x64位系统.2.精简了Ext ...

  3. XML美化工具及其他各种美化工具

    在线工具 http://www.ostools.net/codeformat/xml 3464网页常用工具 http://www.3464.com/Tools/CodeFormat/ 在线工具大全 h ...

  4. 【Android】Android SDK在线更新镜像服务器

    Android SDK在线更新镜像服务器 中国科学院开源协会镜像站地址: IPV4/IPV6: http://mirrors.opencas.cn 端口:80 IPV4/IPV6: http://mi ...

  5. 静态的html页面想要设置使用浏览器缓存

    设置html页面缓存方法: 静态的html页面想要设置使用缓存: 通过HTTP的META设置expires和cache-control code 1.  <meta http-equiv=&qu ...

  6. Android 控件收集

    SwipeMenuExpandableListView   https://github.com/tycallen/SwipeMenu-Expandable-ListView

  7. 编写高性能 Web 应用程序的 10 个技巧

    使用 ASP.NET 编写 Web 应用程序的简单程度令人不敢相信.正因为如此简单,所以很多开发人员就不会花时间来设计其应用程序的结构,以获得更好的性能了.在本文中,我将讲述 10 个用于编写高性能 ...

  8. [ Web Service ] [ SOAP ] [ JSON ] [ XML ] 格式轉換

    JSON格式產生器_Demo JSON格式產生器_ObjGen - Live JSON Generator JSON格式整理_JSON Formatter & Validator Online ...

  9. 如何利用VS2010安装和部署应用程序

    转自:http://jingyan.baidu.com/article/4b52d70255d7f0fc5d774b4d.html 1.假设你当前的winform已经okay了 2.解决方案中新建&q ...

  10. Programming Collective Intelligence

    最近正在拜读 O'reilly出版的Programming Collective Intelligence,准备研究研究搜索引擎了,童鞋们,到时候会考虑公布源码哦!