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

Example:

  1. Input: 3
  2. Output: 5
  3. Explanation:
  4. Given n = 3, there are a total of 5 unique BST's:
  5.  
  6. 1 3 3 2 1
  7. \ / / / \ \
  8. 3 2 1 1 3 2
  9. / / \ \
  10. 2 1 2 3

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

  1. n =
  2.  
  3. n =
  4. / \
  5.  
  6. n =
  7. \ / / / \ \
  8.  
  9. / / \ \

就跟斐波那契数列一样,我们把 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为根的情况,则左子树可以有两个数字,右子树一定不存在)

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

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

解法一:

  1. class Solution {
  2. public:
  3. int numTrees(int n) {
  4. vector<int> dp(n + );
  5. dp[] = dp[] = ;
  6. for (int i = ; i <= n; ++i) {
  7. for (int j = ; j < i; ++j) {
  8. dp[i] += dp[j] * dp[i - j - ];
  9. }
  10. }
  11. return dp[n];
  12. }
  13. };

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

解法二:

  1. class Solution {
  2. public:
  3. int numTrees(int n) {
  4. long res = ;
  5. for (int i = n + ; i <= * n; ++i) {
  6. res = res * i / (i - n);
  7. }
  8. return res / (n + );
  9. }
  10. };

类似题目:

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] 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] 96. Unique Binary Search Trees 唯一二叉搜索树

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

  4. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  5. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  6. [Leetcode] Recover binary search tree 恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  9. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

随机推荐

  1. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

  2. 通过三个DEMO学会SignalR的三种实现方式

    一.理解SignalR ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信(即:客户端(Web页面)和服务器端可以互相实时的通知消息 ...

  3. code

    using System;using System.Threading; namespace ThreadLocalTest{    public class MyObject    {       ...

  4. ThreadLocal<T>的是否有设计问题

    一.吐槽 ThreadLocal<T>明显是.NET从JAVA中来的一个概念,但是这种设计是否出现了问题. 很明显,在JAVA中threadLocal直接是Thread的成员,当然随着th ...

  5. WPF 自定义ContextMenu且为左键点击显示

    <Button Click="Button_Click_3" Style="{StaticResource NormalButtonStyle}"> ...

  6. C# 高效编程笔记1

    C# 高效编程笔记1 1.使用属性而不是可访问的数据成员 (1).NET Framework中的数据绑定类仅支持属性,而不支持共有数据成员 (2)属性相比数据成员更容易修改 2.用运行时常量(read ...

  7. JVM 架构解读

    每个Java开发人员都知道字节码由JRE(Java运行时环境)执行.但许多人不知道JRE是Java Virtual Machine(JVM)的实现,它分析字节码,解释代码并执行它.作为开发人员,我们应 ...

  8. python学习笔记(基础三:if else流程判断、while循环、for循环)

    if else流程判断 getpass在pycharm中无法使用,在命令行窗口中进入python环境可以使用. import getpassusername = input("usernam ...

  9. HttpSession与Hibernate中Session的区别

    一.javax.servlet.http.HttpSession是一个抽象接口 它的产生:J2EE的Web程序在运行的时候,会给每一个新的访问者建立一个HttpSession,这个Session是用户 ...

  10. JS中的对象

    什么事对象?对象是一个整体,对外提供一些操作.而面向对象,就是使用对象时,只关注对象提供的功能,不关注内部的细节,面向对象是一种通用思想. 面向对象编程的特点: 抽象:抓住核心问题: 封装:不考虑内部 ...