题目:

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. jQuery实现列表自动滚动

    需要在页面中一个小的区域循环滚动展示新闻(公告.活动.图片等等),并且,鼠标悬停时停止滚动并提示,离开后,继续滚动. 效果图:    上干货 html: <div id="news&q ...

  2. 分类算法之贝叶斯(Bayes)分类器

    摘要:旁听了清华大学王建勇老师的 数据挖掘:理论与算法 的课,讲的还是挺细的,好记性不如烂笔头,在此记录自己的学习内容,方便以后复习.   一:贝叶斯分类器简介 1)贝叶斯分类器是一种基于统计的分类器 ...

  3. 【转】winform与web 按钮button去掉边框

    ref:http://blog.csdn.net/wangzh300/article/details/5264316 WinForm的话 设置Button属性的FlatStyle为Flat,并且设置F ...

  4. [WinForm]DataGridView列自适应

    关键代码: /// <summary> /// 根据cell内容调整其宽度 /// </summary> /// <param name="girdview&q ...

  5. 一些dos命令

    MS DOS 命令大全 一.基础命令 1 dir 无参数:查看当前所在目录的文件和文件夹. /s:查看当前目录已经其所有子目录的文件和文件夹. /a:查看包括隐含文件的所有文件. /ah:只显示出隐含 ...

  6. event事件:

    onabort: 图像的加载被中断onblur: 元素失去焦点onchange: 域的内容被改变onclick: 当用户点击某个对象时调用的事件句柄ondblclick: 当用户双击某个对象时调用的事 ...

  7. PHP中session的使用

    1.初始化(使用session前都要使用,一个页面用一个就可以了) session_start(); 2.保存 $_SESSION[$sessionName]=$value; (value可以是dou ...

  8. Nat网络地址转换

    Nat中的术语 -------------------------------------------------------------------------------------------- ...

  9. Wpf 简单制作自己的窗体样式(2)

    上一篇blog讲了制作简单的样式的窗体,对于一个传统的窗体,不仅仅可以拖动,和关闭操作.还具有最大化.最小化.隐藏,以及改变窗体的大小等.这篇blog就是对上篇的补充,完善窗体的改变大小和最大化最小化 ...

  10. CodeForces 18C

    Description Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In eac ...