leetCode(26):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
1)结构的总数和存储的n个有序数没有关系。即存放的是1,2,3或5、6、9结果都是一样的;
2)n=0\1时,结果都为1;n个有序数。取不论什么一个作为根结点,该值左边的为左子树。右边的为右子树,
在这样的情况下。可能的结构总数为:左子树结构数*右子树结构数。所以原问题就被分解成了两个子问题;
3)将取n个有序数作为根结点的情况的结构问题相加。就可以得到原问题的解,但应注意的是,如:1、2、3
取1或3作为根结点的情况,结构数是同样的,即具有对称性;那么也要考虑到奇偶性;
4)可以被分解成同样的子问题,自己就行採用递归,本程序採用循环的方法。
*/
int numTrees(int n)
{
vector<int> store;
store.push_back(1);
store.push_back(1);
for(int i=2;i<=n;++i)
{
int tmp=0;
for(int j=0;j<i/2;++j)
{
tmp+=2*store[i-1-j]*store[j];
}
if(i%2)
tmp+=store[i/2]*store[i/2]; store.push_back(tmp);
}
return store[n];
}
leetCode(26):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] 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] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- [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】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
随机推荐
- 周期性任务计划: Crontab
文中部分内容摘自骏马金龙的博客,查看可点击 1. crontab简述 crontab命令用于周期性的执行任务,想要执行这个命令,需要首先启动crond(守护进程)服务才行 crontab是配置管理cr ...
- zabbix 3.4安装
一.server安装 [root@zabbix ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@zabbix ~]# una ...
- Python 模块学习(一)
一.时间模块 import time # 模块调用语句 注意:模块级导入一般放在文件顶部 import datetime print(time.time()) # 1550411181.441547: ...
- LeetCode(15) 3Sum
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- 【URAL 1486】Equal Squares(二维哈希+二分)
Description During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued ...
- LeetCoce 413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- Dev Express中Dock panel的使用
使用DockManager,添加DockPanel. 1,DockManager位于“导航和布局”分类中. 添加一个DockManager控件到窗体中以后,即是在当前窗体类中,添加一个DockMana ...
- LA 4064 (计数 极角排序) Magnetic Train Tracks
这个题和UVa11529很相似. 枚举一个中心点,然后按极角排序,统计以这个点为钝角的三角形的个数,然后用C(n, 3)减去就是答案. 另外遇到直角三角形的情况很是蛋疼,可以用一个eps,不嫌麻烦的话 ...
- spring源码深度解析—Spring的整体架构和环境搭建
概述 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的Java 开发框 ...
- 大数据学习——shell编程
03/ shell编程综合练习 自动化软件部署脚本 3.1 需求 1.需求描述 公司内有一个N个节点的集群,需要统一安装一些软件(jdk) 需要开发一个脚本,实现对集群中的N台节点批量自动下载.安装j ...