[leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数
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的个数的更多相关文章
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 52. leetcode 96. Unique Binary Search Trees
96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...
- 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]* ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- Java [Leetcode 96]Unique Binary Search Trees
题目描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
- 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 ...
- [leetcode] 96 Unique Binary Search Trees (Medium)
原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...
- [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 ...
随机推荐
- 浅析rune数据类型
学习golang基础的时候,发现有个叫rune的的数据类型,当时不理解这个类型的意义. 查询,官方的解释如下: // rune is an alias for int32 and is equival ...
- MongoDB的启动与停止(一)
1:启动和停止Mongodb 1)从命令行启动 执行mongod,启动MongoDB服务器,mongod有很多可配置的启动选项,可以使用mongod --help查看所有选项 -- ...
- 计算:表中varchar类型的字段能容纳的最大字符数?
建表语句: CREATE TABLE `test2` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `content` varchar(21842) NOT ...
- Docker修改本地镜像与容器的存储位置
这个方法里将通过软连接来实现. 首先停掉Docker服务: systemctl restart docker或者service docker stop 然后移动整个/var/lib/docker目录到 ...
- angular的异步处理$q的使用(promise)
Angular中的promise: Promise是一种异步方式处理值的方法.代表了一个函数最 终可能的返回值或者抛出的异常 在之前,通常都是使用闭包或者回调来响应非同步的有意义数据 使用promis ...
- bootstrap模态框弹框后执行Ajax
如下: editModal:模态框ID <script> $(document).ready(function() { $('#editModal').on('hidden.bs.moda ...
- ef join查询
temp = temp.OrderByDescending(s => s.CreateTime).Skip((param.PageIndex - ) * param.PageSize).Take ...
- 第24课 可变参数模板(5)_DllHelper和lambda链式调用
1. dll帮助类 (1)dll的动态链接 ①传统的调用方式:先调用LoadLibrary来加载dll,再定义函数指针类型,接着调用GetProcAddress获取函数地址.然后通过函数指针调用函数, ...
- js中的数值转换
js中有3个函数可以把非数值转换为数值:Number().parseInt().parseFloat().其中Number()可以用于任何数据类型.parseInt()及parseFloat()用于将 ...
- volatile适用场景之二
1.volatile最适用一个线程写,多个线程读的场合. 如果有多个线程并发写操作,仍然需要使用锁或者线程安全的容器或者原子变量来代替.(摘自Netty权威指南) 疑问:如果只是赋值的原子操作,是否可 ...