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. 1 3 3 2 1
  2. \ / / / \ \
  3. 3 2 1 1 3 2
  4. / / \ \
  5. 2 1 2 3

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

  1. 1 n = 1
  2.  
  3. 2 1 n = 2
  4. / \
  5. 1 2
  6.  
  7. 1 3 3 2 1 n = 3
  8. \ / / / \ \
  9. 3 2 1 1 3 2
  10. / / \ \
  11. 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

  1. class Solution {
  2. public int numTrees(int n) {
  3. int[] count = new int[n + 1];
  4.  
  5. count[0] = 1;
  6. count[1] = 1;
  7.  
  8. for (int i = 2; i <= n; i++) {
  9. for (int j = 0; j <= i - 1; j++) {
  10. count[i] = count[i] + count[j] * count[i - j - 1];
  11. }
  12. }
  13.  
  14. return count[n];
  15. }
  16. }   

Python: Math

  1. class Solution(object):
  2. def numTrees(self, n):
  3. if n == 0:
  4. return 1
  5.  
  6. def combination(n, k):
  7. count = 1
  8. # C(n, k) = (n) / 1 * (n - 1) / 2 ... * (n - k + 1) / k
  9. for i in xrange(1, k + 1):
  10. count = count * (n - i + 1) / i;
  11. return count
  12.  
  13. return combination(2 * n, n) - combination(2 * n, n - 1)

Python: DP

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

C++:

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

 

类似题目:

[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. LeetCode初级算法--其他02:有效的括号

    LeetCode初级算法--其他02:有效的括号 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...

  2. es6 字符串模板拼接和传统字符串拼接

    字符串拼接是在日常开发中必不可少的一个环节. 注意:字符串可以用单引号'',或者""双引号,出于方便大家理解,文章以下内容统一使用单引号''! 如果只是一个字符串和一个变量拼接,使 ...

  3. sqlserver2005新特性介绍

    1.更强的编程能力-CLR集成 增强了数据库的编程能力,将一些逻辑层(Bll)转移到数据库中,减少了网络中的数据流量,但是增加了服务器cpu的负荷,当我们需要操作大量的数据,但是产生很少的数据,把这种 ...

  4. IntelliJ IDEA 2019.2破解

    IntelliJ IDEA 2019.2破解 我是参考这个激活的,使用的激活码的方式,需要在百度云盘下载压缩包 https://zhile.io/2018/08/25/jetbrains-licens ...

  5. Cocos2d-x学习小结 配置篇

    Cocos2d-x学习小结 配置篇 学习工具:Cocos2d-x用户手册,<Cocos2d-x游戏开发之旅> 首先官网下载cocos2d-x源码,安装vs2019.如果没有安装python ...

  6. linux查看文件相关命令

    通过命令+文件名查看内容.如下命令可以查看. 1,cat:由第一行开始显示文件内容:一次性显示文件所有内容 2,tac:从最后一行开始显示,可以看出tac与cat字母顺序相反:一次性显示文件所有内容, ...

  7. MongoDB 红宝书-MongoDB官网使用指南

    本文转载自Mongodb中文社区:http://www.mongoing.com/archives/27359 无论你是MongoDB的使用者.爱好者.初学者还是路人甲,有一个学习与进修的资源宝藏是千 ...

  8. HBASE-LSM树(转载)

    HBASE-LSM树 1.B+树 关于B树.B+树.B树的了解参考:* http://blog.csdn.net/v_july_v/article/details/6530142 优点: 走进搜索引擎 ...

  9. P2340 奶牛会展 DP 背包

    P2340 奶牛会展 DP \(n\)头牛,每头牛有智商\(s[i]\)情商\(f[i]\),问如何从中选择几头牛使得智商情商之和最大 且 情商之和.智商之和非负 \(n\le 400,-10^3\l ...

  10. (12)Go面向对象

    尽管Go中没有封装.继承.多态这些概念,但可以通过别的方式实现这个特性: *封装:通过方法实现 *继承:通过匿名字段实现 *多态:通过接口实现 package main import "fm ...