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

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

题意:

给定n个节点,可形成多少种不同的BST

思路:

如果数组为空,毫无疑问,只有一种BST,即空树,          f(0) =1。

如果数组仅有一个元素{1},只有一种BST,单个节点,       f(1) =1。

如果数组有两个元素{1,2}, 那么有如下2种可能,                f(2)=2。

1             2
\ /
2 1

如果数组有三个元素{1,2,3}, 那么有如下5种可能,             f(3)=5。

 1       1           2          3       3
\ \ / \ / /
3 2 1 3 2 1
/ \ / \
2 3 1 2

由此得出规律,

对于任意以i为根节点的二叉树,

其左子树的值一定小于i,也就是[0, i - 1]区间,

而右子树的值一定大于i,也就是[i + 1, n]区间。

假设左子树有m种排列方式,而右子树有n种,则对于i为根节点的二叉树总的排列方式就是m x n

f(2) = f(0) * f(1) + f(1) * f(0);
f(3) = f(0) * f(2) + f(1) * f(1) + f(2) * f(0);
f(4) = f(0) * f(3) + f(1) * f(2) + f(2) * f(1) + f(3) * f(0);
....
f(n) = f(0) * f(n-1) + f(1) * f(n-2) + ... + f(n-2) * f(1) + f(n-1) * f(0); 【卡特兰数(Catalan)】

代码:

 class Solution {
public int numTrees(int n) {
if(n < 1) return 0;
int[] dp = new int[n+1];
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给定节点形成不同BST的个数的更多相关文章

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

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

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

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

  3. 52. leetcode 96. Unique Binary Search Trees

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

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

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

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

  6. Java [Leetcode 96]Unique Binary Search Trees

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

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

  8. [leetcode] 96 Unique Binary Search Trees (Medium)

    原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...

  9. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

随机推荐

  1. java 实现excel 导出功能

    实现功能:java导出excel表 1.jsp代码 <form id="zhanwForm" action="<%=path%>/conferences ...

  2. java多线程同步器

    Java中多线程开发时,离不开线程的分工协作,常用的多线程的同步器有如下几种: 1.CountDownLatch 应用场景:等待一组线程任务完成后在继续执行当前线程. 用法:定义一个CountDown ...

  3. 2.4 Visio2007显示动态对齐网格

  4. 对数据进行GZIP压缩或解压缩

    /** * 对data进行GZIP解压缩 * @param data * @return * @throws Exception */ public static String unCompress( ...

  5. 修改postgres密码

    转载自:https://www.cnblogs.com/kaituorensheng/p/4735191.html   1. 修改PostgreSQL数据库默认用户postgres的密码 Postgr ...

  6. VUE 进行微信支付,解决 微信支付URL未注册

    使用history方式 比较坑吧就不吐槽了,说下实现方式 需要解决问题: 1.因为我的微信支付授权路由是:m.xxxx.com,this.$router.push('xxx')之后经常出现 [微信支付 ...

  7. Mysql数据库主从复制搭建

    Mysql数据库主从复制原理: 主库开启bin-log日志,同时生成IO线程.IO线程负责将用户写入数据库的sql语句记录在二进制日志bin-log,该记录过程可并发进行:生成标识号 server i ...

  8. Flask--(项目准备)--框架搭建,配置文件抽取,业务逻辑抽取

    抽取配置文件: import logging from redis import StrictRedis class Config(object): """项目的配置&q ...

  9. json和数组的区别

    原文地址:https://www.cnblogs.com/zhangjingyun/p/4554054.html 我们都知道,json和数组一样,都可以存数据,但是下面我们来总结一下json和数组的区 ...

  10. Windows下,python pip安装时ReadTimeoutError解决办法

    一般情况下PIP出现ReadTimeoutError都是因为被GFW给墙了,所以一般遇到这种问题,我们可以选择国内的镜像来解决问题. 在Windows下: C:\Users\Administrator ...