leetcode:Unique Binary Search Trees
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
- class Solution {
- public:
- int numTrees(int n) {
- vector<int> num;
- num.push_back(1); //在容器尾端插入一项数据,设置num[0]=1
- for(int i=1; i<=n; i++){
- num.push_back(0); //每次先将num[i]设置为0
- if(i<3)
- num[i]=i; //易知num[1]=1,num[2]=2
- else{
- for(int j=1; j<=i; j++)
- num[i]+=num[j-1]*num[i-j]; //j为root节点,其左子树种数为j-1,右子树种数为i-j
- }
- }
- return num[n];
- }
- };
其他解法:
1、1ms in C++ By Using Theorem From Graph Theory
This is my code. I use the property that the number of unique binary trees or n vertex is
{(2n)(2n-1)(2n-2)....(n+2)}/{(n)(n-1)....(2)(1)}
- class Solution {
- public:
- int numTrees(int n) {
- long long result = 1;
- long long temp = 1;
- for(int i = 2*n; i > n; i--){
- result *= i;
- temp *= (i-n);
- if (result % temp == 0){
- result /= temp;
- temp = 1;
- }
- }
- return result/(n+1);
- }
- };
2、2ms c++ using dp(动态规划)
- class Solution {
- public:
- int numTrees(int n){
- int arr[n+1][n+1];
- memset(arr,0,sizeof(arr));
- for(int len=1; len<=n; len++){
- for(int j=1; j<=n-len+1; j++){
- if(len == 1) arr[len][j] = 1;
- else{
- arr[len][j] += arr[len-1][j+1];
- arr[len][j] += arr[len-1][j];
- for(int k=1;k<len;k++) arr[len][j] += (arr[k][j]*arr[len-k-1][j+k+1]);
- }
- }
- }
- return arr[n][1];
- }
- };
3、
- class Solution {
- public:
- int numTrees(int n) {
- if(n==0) return 0;
- int s[n+1];
- int r;
- s[0] = 1;
- for(int i=1; i<n+1; i++)
- {
- s[i] = 0;
- for(int l=0; l<i; l++)
- {
- r = i-1-l;
- s[i] = s[i]+s[l]*s[r];
- }
- }
- return s[n];
- }
- };
leetcode:Unique Binary Search Trees的更多相关文章
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 96. 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 && Unique Binary Search Trees II
1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
随机推荐
- JSP页面动态联动
效果如图: 页面用法: body部分: 注意:控制层Controller中:
- Hibernate联合主键映射
1.联合主键的映射规则 1) 类中的每个主键属性都对应到数据表中的每个主键列. Hibernate要求具有联合主键的实体类实现Serializable接口,并且重写hashCode与equals方法, ...
- 【前端】js转码
js转码 function urlencode (str) { str = (str + '').toString(); return encodeURIComponent(str).replace( ...
- JavaScript事件委托的技术原理
如今的JavaScript技术界里最火热的一项技术应该是‘事件委托(event delegation)’了.使用事件委托技术能让你避免对特定的每个节点添加事件监听器:相反,事件监听器是被添加到它们的父 ...
- 偏序集的Dilworth定理
定理1 令(X,≤)是一个有限偏序集,并令r是其最大链的大小.则X可以被划分成r个但不能再少的反链.其对偶定理称为Dilworth定理:定理2 令(X,≤)是一个有限偏序集,并令m是反链的最大的大小. ...
- Nagios 快速实现数据可视化的几种方式
Nagios 是一款强大的开源监控软件,但他本身不能绘图,只能查看当前数据,不能看历史数据以及趋势,也正因此,想要更舒适的使用就要搭配绘图软件,现在可搭配的绘图软件有很多,例如 pnp4nagios, ...
- Jmeter测试Mysql
一.在测试计划下,找到Add directory or jar to classpath下填入jdbc驱动路径. 二.新建线程组. 三.在线程组下,添加配置元件—JDBC Connection Con ...
- POJ 2823
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 35941 Accepted: 10636 ...
- 【hadoop】有参考价值的博客整理
好文章的网址: hadoop shuffle机制中针对中间数据的排序过程详解(源代码级) Hadoop mapreduce原理学习 与 Hadoop 对比,如何看待 Spark 技术? 深入理解Had ...
- Linux的别名使用
直接定义别名 编辑当前用户下的.bashrc 文件: vim ~/.bashrc 添加别名为 lmysql 的命令语句 : alias lmysql='mysql -uroot -p -Dtest ...