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. 动态域名作为dga的做法

    https://wenku.baidu.com/view/54b1df373968011ca3009183.html dga算法 import time from ctypes import c_in ...

  2. 4、markdown基本语法

    一.前言 由于有些语法无法在博客园展示,推荐使用Typora解锁全套,下载地址:https://www.typora.io/ 推荐使用jupyter,使用方法:https://www.cnblogs. ...

  3. janusgraph-控制台操作命令

    当顶点数量过多时(我的230w)删除太慢 就用下面的命令, 删除整个图库 graph.close() JanusGraphFactory.drop(graph) 查询所有的顶点属性 用traversa ...

  4. [Codeforces 1251F]Red-White Fence

    Description 题库链接 给你 \(n\) 块白木板,\(k\) 块红木板,分别有各自的长度 \(h_i\).让你用这些木板组成一段围栏,要满足: 只用一块红木板,且所有白木板的长度均严格小于 ...

  5. FFT/NTT [51Nod 1028] 大数乘法 V2

    题目链接:51Nod 传送门 没压位,效率会低一点 1.FFT #include <cstdio> #include <cstring> #include <algori ...

  6. qtcreator cannot find catkin packages

    adding /opt/ros/kinetic to CMAKE_PREFIX_PATH in Project -> build environment only /opt/ros/kineti ...

  7. 2019-2020-1 20199302《Linux内核原理与分析》第八周作业

    一.上课学习笔记 1.shell作用:①运行程序 ②重定向(输入/输出重定向) ③可编程(写脚本) 执行一个c程序时,如果切进另一个进程,会进入该进程而切不回原进程,所以需要为调用的进程创一个子进程. ...

  8. 浅谈前端H5自定义分享实现方法

     引入jweinxin相关js文件,然后才可以做H5的分享 <script src="js/jweixin-1.2.0.js"></script> let ...

  9. STATUS_STACK_BUFFER_OVERRUN不一定是栈缓冲区溢出

    STATUS_STACK_BUFFER_OVERRUN异常一般是指栈缓冲区溢出的溢出,代码为0xC0000409,消息提示一般为“Security check failure or stack buf ...

  10. Prisma 2 is Coming Soon

    转自:https://www.prisma.io/blog/prisma-2-is-coming-soon-mwwfhevie993 Prisma 2 will introduce many fund ...