[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 ...
随机推荐
- zombodb 配置设置
主要是关于es 集群地址以及分片,复制副本的配置,配置主要在postgresql.conf,当然我们可以在函数中指定 postgresql.conf 级别的配置 es 配置 格式 zdb.defaul ...
- DevExpress.XtraGrid.GridControl中数据源的绑定问题
在利用DevExpress.XtraGrid.GridControl作为一个可编辑的表格控件时,在利用控件之前,先将一个初始化的DataTable对象作为GridControl的数据源进行绑定.可是在 ...
- spring找不到bean
有时候明明有bean,spring找不到bean,这时候需要mvn clean下,有时候xml文件不会每次都编译,改了不clean可能不会生效.
- MIDAS.dll 出错时 (Error loading MIDAS.DLL.)
DELPHI 写的程序会出 ---------------------------Pmain---------------------------Error loading MIDAS.DLL.--- ...
- mysqlbinlog基于位置点恢复
基于位置点恢复 /data/mysq/mysqlbin.000026 #mysqlbinlog文件,恢复如下内容: # at 406 #181113 17:15:44 server id 161 e ...
- 【docker】将Java jar文件生成镜像、上传镜像并生成镜像压缩文件
概述 将Springboot的web服务打包成Jar包后,自动化脚本将jar打包成镜像.上传镜像.并生成镜像的压缩文件: Dockerfile FROM 10.254.9.21/library/ora ...
- [转]jvm调优-命令大全(jps jstat jmap jhat jstack jinfo)
运用jvm自带的命令可以方便的在生产监控和打印堆栈的日志信息帮忙我们来定位问题!虽然jvm调优成熟的工具已经有很多:jconsole.大名鼎鼎的VisualVM,IBM的Memory Analyzer ...
- docker内存监控与压测
一直运行的docker容器显示内存已经耗尽,并且容器内存耗尽也没出现重启情况,通过后台查看发现进程没有占用多少内存.内存的监控使用的是cadvisor,计算方式也是使用cadvisor的页面计算方式, ...
- mass create DN
RUN VL10 in the background. http://paperstreetenterprises.com/running-vl10-background/ VL10*开头的TCODE ...
- 【oracle常见错误】oracle监听程序配置/“ORA-12541: TNS: 无监听程序”
问题描述 在用PL/SQL Developer连接Oracle 11g时报错“ORA-12541: TNS: 无监听程序”,如下图所示.可以按照如下的步骤进行解决. 解决方案 监听程序配置 从开始菜单 ...