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
Analysis: Just recursively count the number of left sub tree times number of right sub tree.
class Solution {
public:
int calculate(int n){
int sum = ;
for(int i = ; i< n ;i++ )
sum += tree[i] * tree[n--i] ;
// tree[i] : left subTree tree[n-1-i] " right subTree
return sum ;
}
int numTrees(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
tree.resize(n+,);
for(int i = ; i <= n ; i++)
tree[i] = calculate(i);
return tree[n] ; }
private :
vector<int> tree;
};
LeetCode_Unique Binary Search Trees的更多相关文章
- LeetCode_Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [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 ...
随机推荐
- 单线程Singleton模式的几个要点
1.Singleton模式中的实例构造器可以设置为protected以允许子类派生.2.Singleton模式一般不要支持ICIoneable接口,因为这可能会导致多个对象实例,与Singleton模 ...
- vs2010 suite integration toolkit execution
原因是UltraDeamen的问题,重新换个WinMount来解压ISO文件.完美安装运行
- libeXosip2(3) -- SIP messages and call control API
SIP messages and call control API The SIP messages and call control API. More... Modules eXosip2 INV ...
- 移动web开发研究
1.jQuery Mobile jQuery Mobile框架能够帮助你快速开发出支持多种移动设备的Mobile应用用户界面.jQuery Mobile最新版本是1.4.0,默认主题采用扁平化设计风格 ...
- 【活动】明星衣橱CEO林清华聊创业 | 猎云网
[活动]明星衣橱CEO林清华聊创业 | 猎云网 [活动]明星衣橱CEO林清华聊创业
- Hive 7、Hive 的内表、外表、分区
1.Hive的内表 Hive 的内表,就是正常创建的表,在 http://www.cnblogs.com/raphael5200/p/5208437.html 中已经提到: 2.Hive的外表 创建H ...
- jQuery插件开发 格式与解析
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- 《学习opencv》笔记——矩阵和图像操作——cvCalcCovarMatrix,cvCmp and cvCmpS
矩阵和图像的操作 (1)cvCalcCovarMatrix函数 其结构 void cvCalcCovarMatrix(计算给定点的均值和协方差矩阵 const CvArr** vects,//给定向量 ...
- CentOS6.6修改主机名和网络信息
1.修改主机名称 [root@centos ~]# vim /etc/sysconfig/network #打开文件,修改以下内容并保存 NETWORKING=yes #使用网络HOSTNAME=ce ...
- [CSAPP笔记][第十一章网络编程]
第十一章 网络编程 我们需要理解基本的客户端-服务端编程模型,以及如何编写使用因特网提供的服务的客户端-服务端程序. 最后,我们将把所有这些概念结合起来,开发一个小的但功能齐全的Web服务器,能够为真 ...