leetcode — unique-binary-search-trees
/**
* Source : https://oj.leetcode.com/problems/unique-binary-search-trees/
*
*
* 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
*
*
*/
public class UniqueBinarySearchTree {
/**
* 求出给定n的所有唯一的二叉搜索树
* 二叉搜索树:
* 如果左子树不为空,则左子树的节点值要小于根节点的值,如果右节点不为空,则右子树节点的值大于根节点的值
*
* 找规律
* 设n的唯一二叉搜索树个数为f(n)
* n = 0, f(0) = 1
* n = 1, f(1) = 1
* n = 2, f(2) = 2
* n = 3, f(3) = 5
* .....
*
* 讨论n = 3的情况
* 当根节点为1,左子树必须为空,则总数取决于右子树的个数,f(0)*f(n-1)=f(0)*f(2)=2
* 当根节点为2,左子树为1,右子树为3,在总数为f(1)*(n-2)=f(1)*(1)=1
* 当根节点为3,左子树为空,可能有f(0)*f(2),左子树为1,可能有f(1)*f(1),左子树为2,可能有f(2)*f(0)=2,总共有2+1+2=5
*
* 所以:
* f(0) = 1
* f(n) = f(0)*f(n-1) + f(1)f(n-2) + f(2)*f(n-3) + ... + f(n-3)f(2) + f(n-2)*f(1) + f(n-1)f(0);
*
* 注意:因为要计算f(n),所以数组长度应该为n+1,因为要保存0-n之间的结果
*
*
* @param n
* @return
*/
public int uniqueTreeCount (int n) {
if (n == 0) {
return 1;
}
int[] dp = new int[n+1];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[j] * dp[i-j-1];
}
}
return dp[n];
}
public static void main(String[] args) {
UniqueBinarySearchTree uniqueBinarySearchTree = new UniqueBinarySearchTree();
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(0));
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(1));
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(2));
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(3));
}
}
leetcode — unique-binary-search-trees的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- [leetcode]Unique Binary Search Trees @ Python
原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...
- LEETCODE —— Unique Binary Search Trees [动态规划]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Leetcode Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode: Unique Binary Search Trees [095]
[题目] Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
随机推荐
- 记录一次大量CLOSE_WAIT的情况
近期的项目中,有一个特殊的需求,对于每个客户端程序有若干个机构,对于每个机构有不同的客户端证书,程序间隔一段时间向服务端进行请求,根据请求的成功与否更新各机构的状态(如正常,证书未配置,证书过期等). ...
- Android 动画 (1) 基础
背景 坑, 最近打算在recyclerview item上加一个带动画的button,结果button无法连续点击,还以为是动画是同步的,必须要结束之后才能开始另一个动画,后来去掉recylervie ...
- BZOJ 4804
辣鸡题目毁我青春 易推 \[\sum_{i=1}^n\sum_{i=1}^m \varphi(gcd(i,j))=\sum_{T}\frac{n}{T}\dfrac{m}{T}\sum_{d|T} \ ...
- 详解Session和cookie
1.cookie 1.1. 为什么会有cookie? 由于HTTP是无状态的,服务端并不记得你之前的状态.这种设计是为了HTTP协议的方便,但是也存在一些问题.比如我们登录一个购物网站,我们需要用户登 ...
- 使用Spring Aop自定义注解实现自动记录日志
百度加自己琢磨,以下亲测有效,所以写下来记录,也方便自己回顾浏览加深印象之类,有什么问题可以评论一起解决,不完整之处也请大佬指正,一起进步哈哈(1)首先配置文件: <!-- 声明自动为sprin ...
- vue事件修饰符
阻止单击事件冒泡 <a v-on:click.stop="doThis"></a>提交事件不再重载页面<form v-on:submit.preven ...
- DevOps详解
最近我阅读了很多有关DevOps的文章,其中一些非常有趣,然而一些内容也很欠考虑.貌似很多人越来越坚定地在DevOps与chef.puppet或Docker容器的熟练运用方面划了等号.对此我有不同看法 ...
- prometheus+grafana 监控生产环境机器的系统信息、redis、mongodb以及jmx
介绍: 为了更好的对生产环境的一些中间件和操作系统的运行情况进行可视化的展示,近期了解了下prometheus加上grafana来实现这种效果,由于prometheus是新出来的开源项目,所以,监控的 ...
- Spring + SpringMVC + Mybatis项目中redis的配置及使用
maven文件 <!-- redis --> <dependency> <groupId>redis.clients</groupId> <art ...
- nginx连接数优化
一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1. worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu计 ...