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 ...
随机推荐
- IBMWebsphere 使用jar包删除文件
1. 先使用ant打包一个jar包,删除其他不要的目录和文件,仅保留一个空的xxx.war文件夹("xxx"对应was上的工程安装根目录) 2. 在文件夹下新建一个META-INF ...
- 【HTTP】长连接和短连接
1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议.IP协议主要解决网络路由和寻址问 ...
- HTML学习(2018.1.18)
1, 转义字符 转义字符:用于表示网页中的特殊字符 XHTML不直接输入符号,建议使用转义字符.  ------空格: ©------版权: & ...
- Maven项目pom.xml配置详解
maven项目pom.xml文件配置详解,需要时可以用作参考: <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- 使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中<meta-data>变量的值
转载请说明来源: http://www.cnblogs.com/lizhilin2016/p/7390079.html 最近lz 在开始做一个新的Demo, 在项目中集成了bugly用于收集项目中的崩 ...
- 远程连接Oracle设置
1:打开net manager:开始->程序->oracle->配置和移植工具->Net Manager 2:添加服务器名->名子自定义,一般根据项目自定义,自己知道就行 ...
- JavaScript的基本语法(一)
一.常用的表单元素有: 文本框(text). 密码框(password). 多行文本框(<textarea>) 单选按钮(radio). 复选框(checkbox). 列表框(<se ...
- Object::connect: No such slot (QT槽丢失问题)
1.看看你的类声明中有没有Q_OBJECT,并继承public QMainWindow{ 例如: class CPlot: public QMainWindow{ Q_OBJECT 2.你声明的函数要 ...
- Dart 调用C语言混合编程
Dart 调用C语言本篇博客研究Dart语言如何调用C语言代码混合编程,最后我们实现一个简单示例,在C语言中编写简单加解密函数,使用dart调用并传入字符串,返回加密结果,调用解密函数,恢复字符串内容 ...
- LOJ #6041. 「雅礼集训 2017 Day7」事情的相似度 LCT+SAM+线段树
Code: #include<bits/stdc++.h> #define maxn 200003 using namespace std; void setIO(string s) { ...