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

此题是卡塔兰数的一个应用。注意是BST而不是普通的Binary Tree,所以要满足左比根小,右比根大。

                    1                        n = 1

                2        1                   n = 2
/ \
1 2 1 3 3 2 1 n = 3
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
 
定义f(n)为unique BST的数量,以n = 3为例:
构造的BST的根节点可以取{1, 2, 3}中的任一数字。
如以1为节点,则left subtree只能有0个节点,而right subtree有2, 3两个节点。所以left/right subtree一共的combination数量为:f(0) * f(2) = 2
以2为节点,则left subtree只能为1,right subtree只能为3:f(1) * f(1) = 1
以3为节点,则left subtree有1, 2两个节点,right subtree有0个节点:f(2)*f(0) = 2
总结规律:
f(0) = 1
f(n) = f(0)*f(n-1) + f(1)*f(n-2) + ... + f(n-2)*f(1) + f(n-1)*f(0)

Java: DP

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

Python: Math

class Solution(object):
def numTrees(self, n):
if n == 0:
return 1 def combination(n, k):
count = 1
# C(n, k) = (n) / 1 * (n - 1) / 2 ... * (n - k + 1) / k
for i in xrange(1, k + 1):
count = count * (n - i + 1) / i;
return count return combination(2 * n, n) - combination(2 * n, n - 1)

Python: DP

class Solution2:
def numTrees(self, n):
counts = [1, 1]
for i in xrange(2, n + 1):
count = 0
for j in xrange(i):
count += counts[j] * counts[i - j - 1]
counts.append(count)
return counts[-1]

C++:

class Solution {
public:
int numTrees(int n) {
vector<int> dp(n + 1, 0);
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
dp[i] += dp[j] * dp[i - j - 1];
}
}
return dp[n];
}
};

 

类似题目:

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

All LeetCode Questions List 题目汇总

[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? Example ...

  2. [Leetcode] Unique binary search trees 唯一二叉搜索树

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

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

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

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

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

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

  6. 52. leetcode 96. Unique Binary Search Trees

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

  7. Java [Leetcode 96]Unique Binary Search Trees

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

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

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

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

随机推荐

  1. ArcGIS pro 发布地图服务(一)动态地图服务

    1.软件:arcgis pro 2.4  数据:.mxd文档. 2.导入mxd文档. 3.登录portal账号 4.分析—发布 5.在server中的地图服务 JavaScript api 查看 6. ...

  2. wordpress模板加载顺序汇总

    我们要创建一个新的wordpress模板需要先了解有哪些页面模板,这些页面模板的文件是什么?它们是怎么工作的?下面ytkah汇总了一些常用的wordpress模板结构方便大家查找 首页 首先WordP ...

  3. python中的object

    继承 object 类的是新式类,不继承 object 类的是经典类,在 Python 2.7 里面新式类和经典类在多继承方面会有差异: class A: def foo(self): print(' ...

  4. linux 统计某个文件的行数

    今日思语:迷茫的时候,看看身边那些优秀的人,他们还在那么努力,或许你就可以有点方向和动力了 在linux系统中,我们经常会对文件做行数统计,可以使用如下命令 wc -l file #file为具体的文 ...

  5. C++中字符数组和字符指针问题

    环境:vs2010 说明:在阅读这部分内容之前应该先明确C++内存分配问题 ,那一篇文章说的比较清楚. 1.字符数组,初始化: char str1[]="abc"; char st ...

  6. make 命令出现:"make:*** No targets specified and no makefile found.Stop."

    我们在Linux 安装包的时候,使用make 命令出现:"make:*** No targets specified and no makefile found.Stop."这样的 ...

  7. 文件操作时:xreadlines和readlines的区别?

    二者使用时相同,但返回类型不同,xreadlines返回的是一个生成器,readlines返回的是list

  8. JSON和计算机网络的个人总结

    JSON JSON是什么? JSON:JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于ECMAScript (欧洲计算机协会制定的js规范) ...

  9. 洛谷P4047 [JSOI2010]部落划分题解

    洛谷P4047 [JSOI2010]部落划分题解 题目描述 聪聪研究发现,荒岛野人总是过着群居的生活,但是,并不是整个荒岛上的所有野人都属于同一个部落,野人们总是拉帮结派形成属于自己的部落,不同的部落 ...

  10. 57、Spark Streaming: window滑动窗口以及热点搜索词滑动统计案例

    一.window滑动窗口 1.概述 Spark Streaming提供了滑动窗口操作的支持,从而让我们可以对一个滑动窗口内的数据执行计算操作.每次掉落在窗口内的RDD的数据, 会被聚合起来执行计算操作 ...