【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 ...
随机推荐
- .net core 一次坑爹的类库打包过程
众所周知,.net core 跨平台类库引用一定要通过nuget获得.(如有问题,欢迎指出) 打包 将普通.net project转换成.net core 的类库有两种方式: 1.新建.net cor ...
- ORB-SLAM(一)简介
ORB-SLAM是一种基于ORB特征的三维定位与地图构建算法(SLAM)[1].该算法由Raul Mur-Artal,J. M. M. Montiel和Juan D. Tardos于2015年发表在I ...
- ORB-SLAM(二)性能
ORB-SLAM程序提供了运行Monocular.Stereo和RGBD数据的程序.编译成功后,可以通过运行TUM的标准数据来验证程序是否成功.如果想自己测试一些数据,可以通过OpenCV提供的接口调 ...
- BZOJ 2179: FFT快速傅立叶
2179: FFT快速傅立叶 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2923 Solved: 1498[Submit][Status][Di ...
- 【bzoj3505】 Cqoi2014—数三角形
http://www.lydsy.com/JudgeOnline/problem.php?id=3505 (题目链接) 题意 给定一个n*m的网格,请计算三点都在格点上的三角形共有多少个. Solut ...
- <<< web里面Servlet高级应用的基础介绍
Servlet中的页面跳转?两种方式,实现跳转:内部跳转(请求转发).外部跳转(重定向)内部跳转(请求转发)特点:在服务器内部完成页面之间的跳转:请求只有一次:浏览器地址不会改变.request.ge ...
- <<< Oracle表创建、修改、删除基础操作
表是数据库存储数据的基本单元,它对应于现实世界中的对象(如部门和雇员等).表按列进行定义,存储若干行数据.表中应该至少有一列.在 Oracle 中,表一般指的是一个关系型数据表.也可以生成临时表和对象 ...
- linux REDHAT6.4下安装ArcGIS Server 10.1
1 安装环境 因为Linux的发行版本比较多,我们在使用的时候请严格按照官网给的给出的版本,在官网上给出的是经过严格测试的,如果采用其他的,即便安装上了,在后续的运作中出现问题,这个可就麻烦了,官网对 ...
- Google Map API V3开发(1)
Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...
- PDO防sql注入原理分析
使用pdo的预处理方式可以避免sql注入. 在php手册中'PDO--预处理语句与存储过程'下的说明: 很多更成熟的数据库都支持预处理语句的概念.什么是预处理语句?可以把它看作是想要运行的 SQL 的 ...