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 ...
随机推荐
- Bootstrap3 表格-带边框的表格
添加 .table-bordered 类为表格和其中的每个单元格增加边框. <table class="table table-bordered"> ... </ ...
- 用豆瓣镜像解决pip安装慢的问题
pip3 install django==1.9 -i http://pypi.douban.com/simple/
- Scala:类,对象和特征(接口)
http://blog.csdn.net/pipisorry/article/details/52902609 Scala类和对象 类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象 ...
- Scala:集合类型Collection和迭代器
http://blog.csdn.net/pipisorry/article/details/52902549 Scala Collection Scala 集合分为可变的和不可变的集合. 可变集合可 ...
- Most Common Solutions to FRM-41839 and .tmp Files Not Being Deleted
In this Document Symptoms Changes Cause Solution References APPLIES TO: Oracle Application ...
- Linux内核中的有关Page的算法
static inline int get_order(unsigned long size) { int order; size = (size-1) >> (PAGE_SHIFT-1) ...
- Unity UGUI实现分段式血条
我们可以看到像英雄联盟等游戏里英雄头顶的血条显示并非是纯色的,而是根据血量的多少而显示一定量的格子,这种方式明显是比较友好.比较美观的,事实上我们的游戏里面也想实现这样的效果,那该怎么办呢?根据血量的 ...
- list标准函数的模拟
;反序 ( ) -> ( ) (define (rvs x) (let recur ((x x)(res '())) (if (null? x) res (recur (cdr x) (cons ...
- main函数之后的调用
main函数代表进程的主线程.程序开始执行时,系统为程序创建一个进程,main函数其实并不是首先被调用的函数,而是操作系统调用了C/C++运行期启动函数,该函数负责对C/C++ 运行期库进行初始化.它 ...
- 无网络环境下安装Dynamics CRM
在安装CRM时会需要很多的组件支持,没有这些组件是没法安装的,一般我们都是选择机器联网后在线安装,但也有特殊情况确实不能联网的,可参考这篇文章 https://blogs.msdn.microsoft ...