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 ...
随机推荐
- OpenResty 自定义 access_log 格式
定义access log的format是 Nginx已经提供的功能,有了 ngx_lua 之后就可以更灵活的记录请求相关的信息,而不仅仅拘泥于 Nginx的内置变量了,可以自定义一些格式和变量来存储结 ...
- Maven之(六)setting.xml配置文件详解
setting.xml配置文件 maven的配置文件settings.xml存在于两个地方: 1.安装的地方:${M2_HOME}/conf/settings.xml 2.用户的目录:${user.h ...
- mongo 存储过程
摘要 本文主要介绍mongo存储过程,mongo 存储过程其实就是JS方法,然后通过eval 方法来执行,但是这个方法在3.0 depreate了,也就是在未来的版本,这个功能可能不提供了.从目前的j ...
- Scala:集合类型Collection和迭代器
http://blog.csdn.net/pipisorry/article/details/52902549 Scala Collection Scala 集合分为可变的和不可变的集合. 可变集合可 ...
- springMVC源码分析--SimpleUrlHandlerMapping(四)
上一篇博客springMVC源码分析--AbstractUrlHandlerMapping(三)中我们介绍了AbstractUrlHandlerMapping,主要介绍了一个handlerMap的ur ...
- springMVC源码分析--AbstractHandlerMapping(二)
上一篇博客springMVC源码分析--HandlerMapping(一)中我们简单的介绍了HandlerMapping,接下来我们介绍一下它的抽象实现类AbstractHandlerMapping
- Android自定义View(一、初体验自定义TextView)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51454685 本文出自:[openXu的博客] 目录: 继承View重写onDraw方法 自 ...
- Android Studio 使用wifi调试插件
由于手机亦或是数据线的问题,在应用开发过程中会时不时地遇到手机突然连不上电脑的尴尬时刻,于是就学习了如何使用wifi进行应用调试.下面就具体介绍一下adb wifi插件的安装和使用.其实我们只需要安装 ...
- Java基本语法-----java进制的转换
进制: 进制是一种记数方式 ,可以用有限的数字符号代表所有的数值.由特定的数值组成. 1整型的表现形式 1.十进制: 都是以0-9这九个数字组成,不能以0开头. 2.二进制: 由0和1两个数字组成. ...
- redhat7 上安装dummynet
更多请访问 http://www.webpersonaldeveloper.cn 摘要: 在redhat 上部署dummynet 需要将ipfw 编译为内核模块,而ipfw需要调用linux kern ...