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]* ...
随机推荐
- Jersey 2.x 运行项目
现在我们已经有可以可以运行的项目了,让我们队这个项目进行一些测试吧. 你需要运行下面的一些命令行: mvn clean test 这个命令将会对项目进行编译后运行单元测试. 你应该会看到和下面类似的输 ...
- Jersey 2.x 服务器端应用支持的容器
基于 JAX-RS Servlet-based 部署的一部分标准,能运行在任何支持 Servlet 2.5 和更高标准的的容器上.Jersey 提供支持程序化部署在下面的容器中:Grizzly 2 ( ...
- C++中基类虚析构函数的作用及其原理分析
虚析构函数的理论前提是 执行完子类的析构函数,那么父类的虚构函数必然会被执行. 那么当用delete释放一个父类指针所实例化的子类对象时,如果没有定义虚析构函数,那么将只会调用父类的析构函数,而不会调 ...
- VS2013/2015 html 设计视图窗口
- iOS UI-Lable标签、NStimer定时器和RunLoop超级死循环
// 标签UILable -显示文字 // 1.创建标签 UILabel *lable = [[UILabel alloc] init]; // 2.设置标签的坐标和大小 [lable setFram ...
- win7下安装node及出现的npm问题
按照官网下载安装,选择 Windows Installer (.msi):,一直next安装,默认安装在C:\Program Files\nodejs下,环境变量会自动添加 如果安装后,打开cmd输入 ...
- LD_PRELOAD的偷梁换柱之能
作者: net66 原创 本文网址:http://www.cnblogs.com/net66/p/5609026.html 发布日期:2015 年 06月 22日 一.LD_PRELOAD是什么 LD ...
- PHP:第一章——PHP中的魔术常量
<?php //__LINE__输出常量所在的行 //echo __LINE__; //2.__FILE__常量返回文件的完整路径和文件名; //echo __FILE__; //3.__DIR ...
- webservice 基本要点
webservice的特点 webservices是自我包含的 webservices是自我描述的 webservices是跨平台和语言的 webservices是基于开放和标准的 webservic ...
- DevExpress v18.1新版亮点——ASP.NET篇(一)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress ASP.NET v18.1 的新功能,快来下载试用新版本!点 ...