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 ...
随机推荐
- CentOS7 搭建Kafka(二)kafka篇
CentOS7 搭建Kafka(二)kafka篇 前面我们说了zookeeper的搭建,zookeeper运行后就可以着手搭建kafka了. 必看 喜欢官方文档的请移步:[http://kafka.a ...
- HDU 4901 DP
我觉得这个DP挺难的...然而这只是lydrainbowcat学长幻灯片上的第一题-- 明天考试要GG. 题意: 给你一个序列,让你选出两个集合S和T.保证S里的数都在T里的数的左边.求一共有多少个集 ...
- CAGradientLayer 颜色渐变实现进度条
#import <UIKit/UIKit.h> @interface TJGradientProgressView : UIView /** * 进度值 */ @property(nona ...
- [PHP][学习笔记][CURL]监测设备运行情况小demo
1.curl获取的web content 不能直接echo到页面,会造成js各种错误 2.想办法处理字符串的截取.拼接 2.1.裁剪html返回的字符串 function cutStringFrom( ...
- VMWare 在物理机待机后,报错“该虚拟机似乎正在使用中”
在物理机待机后,刚打开虚拟机,就弹出这个画面(这种情况经常出现在远程之后,本机待机之后) 点击确定后,就弹出 当点击取消,无反应,而且再次点击VM2又弹出以上窗口,点击获取所有权,则弹出以下窗口 上网 ...
- macOS下登录store或者xcode等应用时提示【this action could not be completed】
sudo mkdir -p /Users/Shared sudo chown root:wheel /Users/Shared sudo chmod -R 1777 /Users/Shared === ...
- 从柯洁对战AlphaGo,看商业智能
[摘要]李开复赛前说,AlphaGo和李世石的人机大战是第一次,可能还有悬念,那今天的AlphaGo已经在围棋的世界中彻底甩开了人类,不再拥有任何其他的可能.并指出,AlphaGo和柯洁的比赛并非没有 ...
- 我的最爱Lambda演算——开篇
(在这个帖子的原始版本里,我试图用一个JavaScript工具来生成MathML.但不太顺利:有几个浏览器没法正确的渲染,在RSS feed里也显示的不好.所以我只好从头开始,用简单的文本格式重新写一 ...
- JQuery的click,trigger触发a标签的click事件无效的问题分析
今天在做一个手机端webAPP链接下载的时候,给a标签一个下载链接,但是通过 <a id="downFile" download="" href=&quo ...
- Django解决跨域俩方案
方案一:一套干掉全部Primary 首先你的pip下载一个第三方库贼厉害的: pip install corsheaders 然后在项目的setting.py里面引入第三方库,如下: INSTALLE ...