【leetcode】Unique Binary Search Trees (#96)
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 1 2 3 3
\ \ / \ / /
3 2 1 3 2 1
/ \ / \
2 3 1 2
可以看到,以 1 为根的树的个数,等于左子树的个数乘以右子树的个数,左子树是 0 个元素的树,右子树是 2 个元素的树。以 2 为根的树的个数,等于左子树的个数乘以右子树的个数,左子树是1个元素的树,右子树也是1个元素的树,以此类推。当数组为 1,2,3,...,n 时,基于以下原则的构建的BST树具有唯一性:以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1,n]构成。
定义f(i)为以[1,i] 能产生的 UniqueBinarySearchTree 的数目,则有
如果数组为空,毫无疑问,只有一种 BST,即空树,f(0) = 1。
如果数组仅有一个元素 1,只有一种 BST,单个节点,f(1) = 1。
如果数组有两个元素 1,2,那么有如下两种可能
1 2
\ /
2 1
f(2) = f(0)∗f(1) ,1 为根的情况
+ f(1)∗f(0) ,2 为根的情况
再看一看 3 个元素的数组,可以发现 BST 的取值方式如下:
f(3) = f(0)∗f(2) ,1 为根的情况
+ f(1)∗f(1) ,2 为根的情况
+ f(2)∗f(0) ,3 为根的情况
所以,由此观察,可以得出f的递推公式为
f(i) =∑f(k−1)×f(i−k)
因此,可以利用动态规划解决此问题:
/*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
*/
//#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <climits>
#include <ctime>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cstdlib>
#include <windows.h>
#include <string>
#include <cstring> using namespace std;
class Solution {
public:
int numTrees(int n) {
vector<int>f(n+1,0);
f[0] = 1;
f[1] = 1;
for(int i=2; i<=n; i++){
for(int j=1; j<=i; j++){
f[i] +=f[j-1]*f[i-j];
}
}
return f[n];
}
}; int main(){
Solution s;
cout<<s.numTrees(3)<<endl;
system("pause");
return 0;
}
【leetcode】Unique Binary Search Trees (#96)的更多相关文章
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
- 【leetcode】 Unique Binary Search Trees II (middle)☆
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【题解】【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】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【Leetcode】【Medium】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【Leetcode】【Medium】Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【Leetcod】Unique Binary Search Trees II
给定结点数n,结点值为1,2,...,n,求由这些结点可以构成的所有二叉查找树. Given n, generate all structurally unique BST's (binary sea ...
随机推荐
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7676 Solved: 3509[Subm ...
- bzoj3033
欧拉路,这题好神啊QAQ 显然选择的方案数有2^n种,因为每个点度数都为二所以肯定是一条欧拉路, 第二问直接爆搜即可... ----然而我并没有想到---- 第一问我推出来了(别问我怎么推的,我说我是 ...
- 时间戳 时区 java mysql
当一个时间 比如2016年5月6日,生成时间戳.这个运算是与时区有关的.首先得确认这个时间是哪个时区的,然后转换成utc时区的时间.再减去1970,得到的秒数,就是时间戳. 时间戳是个一定的值,他与时 ...
- asp.net读取execl模板并填充数据,关闭进程
<head runat="server"> <title></title> <script src="Scripts/jquer ...
- 纯js+css实现loading等待效果
此插件是基于jqueryUI的widget,下面是具体实现代码 第一部分css: /***loading***/ .loading-box{ position:absolute; text-align ...
- Java学习笔记11
package welcome; import java.util.Scanner; /* * 代数问题:求解2x2线性方程 */ public class ComputeLinearEquation ...
- Redis【知识点】批量删除指定Key
Redis中有删除单条数据的命令DEL但是他没有批量删除多条数据的方法,那我们怎么去批量删除多条数据呢! 第一种方式 /work/app/redis/bin/redis-cli -a youpassw ...
- junit学习笔记
junit编程规范 测试方法上必须使用@Test进行修饰 测试方法必须使用public void 进行修饰,不能带任何的参数 新建一个源代码目录 测试类的包应该和被测试类保持一致 测试单元中的每个方法 ...
- 2015.4.19 为什么footer下a的索引值那么大
1.问题demo:为什么footer下a的索引值那么大,index不是查找兄弟级别的元素么?而且还限定了范围在footer下的a的情况下. 解决方法:alert( $("#footer a& ...
- Android检测网络是否正常代码!
在Android开发中,如果该应用程序需要连接网络请求,那么最好我们先做一个检测网络是否在线的判断,否则程序容易出现卡死或FC等Bug,应该判断如果手机离线则弹出提示让用户检查网络,如果正常则继续执行 ...