leetcode -day28 Unique Binary Search Trees I II
1、
Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.
For example,
Given n = 3, your program should return all 5 unique BST's shown below.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
分析:本题非常easy想到用递归的方法,依次将每一个点作为根结点,然后递归左右子树,可是在这个递归中有个问题,怎样推断递归结束,怎样保留上层的结点,怎样推断递归的是左子树还是有子树,递归的写法还是非常重要的。本题中,採用每次先求得下层子树左右子树的结点,然后依据左右子树的不同情况形成不同的树,保存这些树的根结点。
代码例如以下:
class Solution {
public:
vector<TreeNode *> generateTrees(int n) {
return constructTree(0,n-1);
}
vector<TreeNode*> constructTree(int startIndex, int endIndex){
vector<TreeNode *> result; //保存下一层的结点
if(startIndex > endIndex){
result.push_back(NULL);
return result;
}
for(int i=startIndex; i<=endIndex; ++i){
vector<TreeNode*> leftTrees = constructTree(startIndex,i-1);
vector<TreeNode*> rightTrees = constructTree(i+1,endIndex);
for(int j=0; j<leftTrees.size(); ++j){
for(int k=0; k<rightTrees.size(); ++k){
TreeNode * newNode = new TreeNode(i+1);
result.push_back(newNode);
newNode->left = leftTrees[j];
newNode->right = rightTrees[k];
}
}
}
return result;
}
};
2、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) {
if(n < 1){
return 1;
}
return numTrees(1,n);
}
int numTrees(int startIndex, int endIndex){
int num = 1;
if(startIndex > endIndex){
return num;
}
num = 0;
for(int i=startIndex; i<=endIndex; ++i){
int leftNum = numTrees(startIndex,i-1);
int rightNum = numTrees(i+1,endIndex);
num += leftNum * rightNum;
}
return num;
}
};
leetcode -day28 Unique Binary Search Trees I II的更多相关文章
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- [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 ...
- 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:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- [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 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
随机推荐
- ubuntu svn二进制文件
1. 查找2:04时间的日志文件和position. Ps:这里假设我找到的是 mysql-bin.000065 位置开始为1356. 2 复制最近的几个日志文件,从mysql-bin.000065 ...
- 最小生成树(模板 prim)
Description 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表, 表中列出了任意两城镇间修建道路 ...
- 11g adg 环境搭建实施手册-0908
11g adg 环境搭建实施手册-0908 2017年8月30日 9:16 11g adg 环境搭建实施手册-0824 2017年8月24日 10:18 ####################### ...
- 在Windows下配置svn服务端钩子程序(部分)
需求一,svn提交时必须填写log日志的需求,如何进行配置呢?请看下面. 需要在版本库目录下找到hooks文件夹,我的版本库是dxoffice,所以是这个目录,你要找自己的目录 然后进入,创建一个pr ...
- php中的curl的一些参数总结
curl可以根据是否是http或则是https选择加密发送的内容: 使用curl发送请求的基本流程 1,初始化连接句柄: 2,设置curl选项: 3,执行并获取结果: 4,释放curl连接句柄: 例子 ...
- 转mysql半主从同步
MySQL半同步复制 从MySQL5.5开始,MySQL以插件的形式支持半同步复制.如何理解半同步呢?首先我们来看看异步,全同步的概念 异步复制(Asynchronous replication) ...
- Hive时间函数笔记
unix_timestamp()函数: 返回值: bigint说明: 获得当前时区的UNIX时间戳 举例: hive> select unix_timestamp() from dual; 14 ...
- Fragment利用ViewPager实现左右滑动--第三方开源--SlidingTabLayout和SlidingTabStrip实现
MainActivity: package com.zzw.fragmentteb; import java.util.ArrayList; import android.graphics.Color ...
- Java——线程池
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- freeradius的https查询功能
一.服务器要求 Radius服务器:centos6.6.ip.hostname.selinux disabled.stop iptables freeradius版本:3.0.12 二.源码安装fr ...