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

Example:

Input: 3
Output: 5
Explanation:
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

这道题实际上是 卡塔兰数 Catalan Numbe 的一个例子,如果对卡塔兰数不熟悉的童鞋可能真不太好做。话说其实我也是今天才知道的好嘛 -.-|||,为啥我以前都不知道捏?!为啥卡塔兰数不像斐波那契数那样人尽皆知呢,是我太孤陋寡闻么?!不过今天知道也不晚,不断的学习新的东西,这才是刷题的意义所在嘛! 好了,废话不多说了,赶紧回到题目上来吧。我们先来看当 n = 1 的情况,只能形成唯一的一棵二叉搜索树,n分别为 1,2,3 的情况如下所示:

                                            n = 

                                           n =
/ \ n =
\ / / / \ \ / / \ \

就跟斐波那契数列一样,我们把 n = 0 时赋为1,因为空树也算一种二叉搜索树,那么 n = 1 时的情况可以看做是其左子树个数乘以右子树的个数,左右子树都是空树,所以1乘1还是1。那么 n = 2 时,由于1和2都可以为根,分别算出来,再把它们加起来即可。n = 2 的情况可由下面式子算出(这里的 dp[i] 表示当有i个数字能组成的 BST 的个数):

dp[2] =  dp[0] * dp[1]   (1为根的情况,则左子树一定不存在,右子树可以有一个数字)

    + dp[1] * dp[0]    (2为根的情况,则左子树可以有一个数字,右子树一定不存在)

同理可写出 n = 3 的计算方法:

dp[3] =  dp[0] * dp[2]   (1为根的情况,则左子树一定不存在,右子树可以有两个数字)

    + dp[1] * dp[1]    (2为根的情况,则左右子树都可以各有一个数字)

     + dp[2] * dp[0]    (3为根的情况,则左子树可以有两个数字,右子树一定不存在)

由此可以得出卡塔兰数列的递推式为:

我们根据以上的分析,可以写出代码如下:

解法一:

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

由卡特兰数的递推式还可以推导出其通项公式,即 C(2n,n)/(n+1),表示在 2n 个数字中任取n个数的方法再除以 n+1,只要你还没有忘记高中的排列组合的知识,就不难写出下面的代码,注意在相乘的时候为了防止整型数溢出,要将结果 res 定义为长整型,参见代码如下:

解法二:

class Solution {
public:
int numTrees(int n) {
long res = ;
for (int i = n + ; i <= * n; ++i) {
res = res * i / (i - n);
}
return res / (n + );
}
};

类似题目:

Unique Binary Search Trees II

Different Ways to Add Parentheses

参考资料:

https://leetcode.com/problems/unique-binary-search-trees/

https://leetcode.com/problems/unique-binary-search-trees/discuss/31666/DP-Solution-in-6-lines-with-explanation.-F(i-n)-G(i-1)-*-G(n-i)

https://leetcode.com/problems/unique-binary-search-trees/discuss/31671/A-very-simple-and-straight-ans-based-on-MathCatalan-Number-O(N)-timesO(1)space

LeetCode All in One 题目讲解汇总(持续更新中...)

[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] 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. python 中in 的 用法

    1.   作用为 成员运算符   在字符串内操作,如果字符串包含相关字符 则返回True,如果不包含则返回False   当然处理不单单是只有单个字符,多个连续的字符也是可以处理的 # 单个字符 a= ...

  2. ubuntu 16.04 上编译和安装C++机器学习工具包mlpack并编写mlpack-config.cmake | tutorial to compile and install mplack on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/1cd6a04d/,欢迎阅读最新内容! tutorial to compile and install mplack on ubun ...

  3. 记录一个 C# 导出 Excel 的坑

    Ø  简介 其实该文章并没有什么技术含量,只是记录下个人心得.在以后有朋友遇到类似问题,可以借鉴下,或者遇到相同问题时的提供个参考方向. 也算我的一个经历吧,这个问题我花了足足一天多的时间才找到问题, ...

  4. AngleSharp 实战(03)之遍历内部子元素

    文档地址:https://anglesharp.github.io/docs/Examples.html 直接贴代码了: using System; using System.Linq; using ...

  5. WPF Button IsMouseOver Background

    https://stackoverflow.com/questions/17259280/how-do-you-change-background-for-a-button-mouseover-in- ...

  6. C# NuGet常用命令

    命令执行位置:工具=〉Nuget包管理器=〉程序包管理器控制台 一.安装 1.安装指定版本类库install-package <程序包名> -version <版本号> 2.安 ...

  7. 「黑客必备技能」Python正则表达式详解

    说在前面 正则表达式是一个很强大的字符串处理工具,几乎任何关于字符串的操作都可以使用正则表达式来完成,作为一个爬虫工作者,每天和字符串打交道,正则表达式更是不可或缺的技能. 正则表达式在不同的语言中使 ...

  8. 【IPHONE开发-OBJECTC入门学习】对象的归档和解归档

    转自:http://blog.csdn.net/java886o/article/details/9046967 #import <Foundation/Foundation.h> #im ...

  9. 简单的基于promise的ajax封装

    基于promise的ajax封装 //调用方式: /* ajaxPrmomise({ url:, method:, headers:{} }).then(res=>{}) */ ;(functi ...

  10. c# 第9节 数据类型之值类型

    本节内容: 1:数据类型是什么 1:数据类型是什么 2:数据类型--值类型 3:值类型和引用类型的区分 画图现象: 3:值类型的种类 整数: 浮点数: 字符: