LeetCode96_Unique Binary Search Trees(求1到n这些节点能够组成多少种不同的二叉查找树) Java题解
题目:
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
解题:
用递归的思想,当仅仅有0个或是1个节点的时候。仅仅有一种。n个节点的时候有f(n)种:
左边能够有n-1个节点,右边0个节点,依据对称性能够左右互换。这时候有2*f(n-1)*f(0);
一边1个,还有一边n-2个,这时候有2*f(1)*f(n-2);
一边两个,一边N-3个,这时候有2*f(2)*f(n-3);
。。。
。。。
假设n为奇数,两边都是n/2个。这时候有f(n/2)*f(n/2),假设n为偶数,一边n/2一边(n/2+1)个,为2*f(n/2)*f(n/2+1);
代码:
public static int numTrees(int n) {
if(n==1||n==0)
return 1;
int sum=0;
if(n%2==0)
{
for(int k=n-1;k>=n/2;k--)
{
sum+=2*numTrees(k)*numTrees(n-1-k);
}
return sum; }
else {
for(int k=n-1;k>n/2;k--)
{
sum+=2*numTrees(k)*numTrees(n-1-k);
}
return sum+numTrees(n/2)*numTrees(n/2);
} }
LeetCode96_Unique Binary Search Trees(求1到n这些节点能够组成多少种不同的二叉查找树) Java题解的更多相关文章
- 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 I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- 【题解】【BST】【Leetcode】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 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 ...
- 96. Unique Binary Search Trees
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- Unique Binary Search Trees——LeetCode
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【Leetcod】Unique Binary Search Trees II
给定结点数n,结点值为1,2,...,n,求由这些结点可以构成的所有二叉查找树. Given n, generate all structurally unique BST's (binary sea ...
- LeetCode OJ 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 ...
随机推荐
- ES6 学习小结1
ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript 2015. 也就是说,ES6就是ES2015. ...
- 如何用js代码实现图片切换效果
通过点击按钮,实现图片的隐藏与显现,切换. 实现代码:<style> .a{ width: 300px; height: 300px; border: 1px solid black; } ...
- Eclipse之注释部分代码
有时为了调试,部分代码需要注释掉,如果一行一行的加注释符特别麻烦. 一.快捷键:ctrl+/ 选择需要注释的部分,点Ctrl+/,这段代码前,自动加注释语. 取消注释也是按Ctrl+/ 步骤: 选择需 ...
- QT-Creator+SDK+编译器+自定义配置
QT4.8的软件曾经耗费巨大的功夫进行构建,不舍得扔掉!重新安装Qt4.8版本 1.安装qt-creator 安装qt-creator-win-opensource-2.4.0.exe版本,不建议使用 ...
- VR: AR和VR演进哲学
Facebook 20亿美元(4亿美元+16亿美元股票换购方式)收购虚拟现实厂商Oculus 引爆AR产业,索尼不温不火逐步演进的头盔项目也该加速了.最近Oculus rift发布了商业版本:Ocul ...
- 我的web前端自学之路-心得篇:我为什么要学习web前端?
时光如流水,转眼间,自己已经是大三的学长了,看着一个个学弟学妹,心中有种莫名的感觉,很怀念大学的前两年时光,但也很憧憬着自己的未来,自己将要去经历很多从未经历的事.我是我们学校信科院的一名学生,在编程 ...
- nginx + php 403 原因分析
环境:nginx + php 问题: 配置的网站,访问出现报错:Access Denied (403) 常见解决方法: 1.文件权限问题 可能是文件权限问题,没有读权限. 或者selinux没有关闭. ...
- java操作Excel的poi的导出Excel表格
页面布局 点击导出用户:触发函数,直接访问后台 后台方法如下: public String export()throws Exception{ Connection con=null; try { c ...
- 明明引用了jquery,js还是报错
先引jquery,不然加载上一个js的时候jquery还没有加载 <script src="js/jquery-1.9.1.js" type="text/javas ...
- scrapy-redis使redis不止保存url
先看scrapy-redis源码 class RedisMixin(object): """Mixin class to implement reading urls f ...