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 ...
随机推荐
- Linux启动流程CentOS6和7的区别
目 录 Linux启动流程 I 第1章 CentOS6启动流程 1 1.1 BIOS 1 1.2 MBR 1 1.3 GRUB 1 1.4 kernel(加载内核) ...
- 树莓派 - 蓝牙 (1) 试试Beacon
首先先了解一下bluez, 以及常用的tools. - hcitool.bluetoothctl等工具,可以进行BLE设备的扫描.连接.配对.广播等操作: - hcitool可以发送HCI comma ...
- iptables:ipset批量管理ip
1.安装 安装: yum -y install ipset \apt-get -y install ipset 2.创建一个ipset ipset create whitelist hash:net ...
- Binlog详解
一.介绍 binlog, 即二进制文件,他记录了MySQL所有数据的变更,并以二进制的形式存储在磁盘上 二.binlog模式 binlog有三种模式:ROW(行模式), Statement(语句模 ...
- POJ 4118 开餐馆
Description 北大信息学院的同学小明毕业之后打算创业开餐馆.现在共有n 个地点可供选择.小明打算从中选择合适的位置开设一些餐馆.这 n 个地点排列在同一条直线上.我们用一个整数序列m1, m ...
- 微信小程序显示cms里的html文章
首先在cms模版中将html文章转化为json数据,识别图片,文本和换行,过滤掉样式和标签.这里是用PHP的正则表达式函数来实现的,$content是cms里的html文章. <?php $_a ...
- 分享21个基于jquery菜单导航的效果
jquery导航菜单插件制作jquery动画菜单熔岩灯菜单效果更新时间:02月15日 14:53:03 虾米精选-菜单导航-导航菜单 0浏览 / ★★★☆☆星级 / 未知软件大小/ jquery导航菜 ...
- python3--算法基础:二分查找/折半查找
算法基础:二分查找/折半查找 #!/usr/bin/env python # -*- coding:utf-8 -*- # 算法基础:二分查找/折半查找 def binarySearch(dataSo ...
- 【HDOJ6330】Visual Cube(模拟)
题意: 思路: import java.util.Scanner; public class Main { public static void main(String args[]) { Scann ...
- python学习之-- 事件驱动模型
目前主流的网络驱动模型:事件驱动模型 事件驱动模型:也属于生产者/消费者结构,通过一个队列,保存生产者触发的事件,队列另一头是一个循环从队列里不断的提取事件.大致流程如下:1:首先生成一个事件消息队列 ...