Leetcode_96_Unique Binary Search Trees
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43198929
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
思路:
(1)题意为给定n个节点,求能组成多少个二叉树。
(2)该题和给定n个数字,求其所有进栈出栈顺序总个数是相同的,详情参看数据结构。
(3)本题是运用递归进行实现。
递推关系为: f(n)=C(2n,n)/(n+1) (n=1,2,3,...)。
递归式为: f(n)=f(n-1)*(4*n-2)/(n+1)。
通过该公式进行递归即可得到答案。但是通过递归实现的算法效率显然要低一些。
递推过程:
当n=1时,只有1个根节点,则能组成1种,令n个节点可组成的二叉树数量表示为f(n), 则f(1)=1;
当n=2时,1个根节点固定,还有n-1个节点,可以作为左子树,也可以作为右子树, 即:f(2)=f(0)*f(1)+f(1)*f(0)=2,则能组成2种。
当n=3时,1个根节点固定,还有n-1=2个节点,可以在左子树或右子树, 即:f(3)=f(0)*f(2)+f(1)*f(1)+f(2)*f(0)=5,则能组成5种。
当n>=2时,有f(n)=f(0)*f(n-1)+f(1)*f(n-2)+...+f(n-1)*f(0)
(4)希望本文对你有所帮助。
算法代码实现如下:
/**
* @author liqq
*/
public int numTrees(int n) {
int i;
int result = 0;
if(n==0) return 1;
if(n==1) return 1;
for (i = n-1; i>=0 ; i--) {
result = result + numTrees(i)*numTrees(n-i-1);
}
return result;
}
Leetcode_96_Unique 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 ...
- 2 Unique Binary Search Trees II_Leetcode
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【leetcode】Unique Binary Search Trees (#96)
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】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- Leetcode 86. Unique Binary Search Trees
本题利用BST的特性来用DP求解.由于BST的性质,所以root左子树的node全部<root.而右子树的node全部>root. 左子树 = [1, j-1], root = j, 右子 ...
- Print Common Nodes in Two Binary Search Trees
Given two Binary Search Trees, find common nodes in them. In other words, find intersection of two B ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
随机推荐
- MacOS下对postgresql的简单管理操作
如何安装在另一篇blog中有述,这里不再赘述.本篇简单说一下安装完postgresql之后的一些管理和查询操作. 首先安装完postgresql之后需要初始化数据库: initdb /usr/loca ...
- Python 好用得让人发指的函数参数语法糖
是吗? def f(x,y,z): return x+y+z t=(200,300) print(f(1,2,3)) print(f(1,*t)) 结果: >>> 6 501 这是我 ...
- JAVA面向对象-----包机制
JAVA面向对象-–包机制 问题: 当定义了多个类的时候,可能会发生类名的重复问题. 在java中采用包机制处理开发者定义的类名冲突问题. 怎么使用java的包机制呢? 1.使用package 关键字 ...
- “出错了”和报告Bug的艺术
"出错了." 没有那句话能像"出错了"一样让程序员/开发者如此沮丧,心里翻江倒海,怒火一点即燃,还要死掉一大片脑细胞. 这句生硬的开场白通常标志着让开发者恐惧的 ...
- 剑指offer-面试题7:俩个栈实现队列(java)
详细分析请参照C语言版,这里仅仅给出实现代码,注释很详细,不得不说java各种api用起来真是爽飞了 1 package com.xsf.SordForOffer; 2 3 import java.u ...
- 02_IO操作的基本规律(InputStream,OutputStream,Reader,Writer,FileReader,FileWriter,BufferedReader,BufferedWri
模拟BufferedInputStream,编写一个类 package toto.IO; import java.io.IOException; import java.io.InputStre ...
- Android项目开发填坑记-so文件引发的攻坚战
故事的最初 我负责的项目A要求有播放在线视频的功能,当时从别人的聊天记录的一瞥中发现百度有相关的SDK,当时找到的是Baidu-T5Player-SDK-Android-1.4s,项目中Demo的so ...
- Android初级教程:shape的基本用法
转载本文请注明出处:http://blog.csdn.net/qq_32059827/article/details/52203347 点击打开链接 在自定义进度条之前,先来学习一下shape的用 ...
- mac os X下的updatedb
unix或linux下使用locate指令在其数据库中查询文件,使用updatedb可以 更新locate的数据库.而在mac os X下却找不到updated这个程序.使用 man locate查看 ...
- Android开发学习之路--Activity之初体验
环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...