[Leetcode] 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
confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".
题中很重要的一点是,各个结点的值是递增的,即非降型。所以,对任一点,其前的点构成二叉树的左子树,其后点构成二叉树右子树。可以从这两部分中随意的选取一部分组成子二叉树的左右子树。递归方法的思路是,遍历这n个数,然后从当前数的前后部分选取、组合成新的二叉树。终止条件是beg>end。感觉很哄哄的思想-水中的鱼的博客,用指针及堆来存储变量。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode *> generateTrees(int n)
{
return creatTre(,n);
} vector<TreeNode *> creatTre(int beg,int end)
{
vector<TreeNode *> res;
if(beg>end)
{
res.push_back(NULL);
return res;
}
for(int k=beg;k<=end;++k)
{
//求根节点i的左右子树集合
vector<TreeNode *> lTree=creatTre(beg,k-);
vector<TreeNode *> rTree=creatTre(k+,end); /*将左右子树相互匹配,每一个左子树都与所有右子树匹配,
*每一个右子树都与所有的左子树匹配 */
for(int i=;i<lTree.size();++i)
for(int j=;j<rTree.size();++j)
{
TreeNode *root=new TreeNode(k);
root->left=lTree[i];
root->right=rTree[j];
res.push_back(root);
}
}
return res;
}
};
[Leetcode] Unique binary search trees ii 唯一二叉搜索树的更多相关文章
- [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] 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 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
[096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...
- Leetcode96.Unique Binary Search Trees不同的二叉搜索树
给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (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. F ...
- LeetCode——Unique Binary Search Trees II
Question Given an integer n, generate all structurally unique BST's (binary search trees) that store ...
随机推荐
- Java写Excel(不生成实体文件,写为流的形式)
java 写 Excel(不生成实体文件,写为流的形式) public String exportReportExcel(String mediaCode, List<SimpleMediaRe ...
- git push之后回滚(撤销)代码
问题描述:首先,先说明一下,为什么会引发这次的话题,是这样的,我做完功能Agit push之后,2个月后需求部门要求不要功能A了,然后需要在没有功能A的基础上开发,怎么办?赶紧回滚代码呀. 然后我用g ...
- 【tp5.1】七牛云上传图片
composer安装: composer require qiniu/php-sdk 配置使用: 在tp5.1的配置文件app.php中配置七牛云的参数 'qiniu' => [ 'access ...
- 谭浩强C语言第四版第九章课后习题7--9题(建立,输出,删除,插入链表处理)
#include<stdio.h> #include<stdlib.h> #define N sizeof(link) typedef struct stu { struct ...
- My First Marathon【我的第一次马拉松】
My First Marathon A month before my first matathon, one of my ankles was injured and this meant not ...
- urllib,url中链接包含汉字怎么用百分号(%)加密处理
使用urllib中的quote,和unquote方法将汉字编码成gbk(2个百分号对应一个汉字)或者utf8(3个百分号对应一个汉字) 注意用%加密汉字时,汉字不能是Unicode编码格式,否则会报错 ...
- Oozie Coordinator job 之定时任务
使用 Coordinator job 可以执行定时任务和时间触发执行 需要注意的是 Oozie 默认使用的时区与中国时区不是一致的,需要进行一点修改 1.关于时区 a.修改 core-site.xml ...
- 11 TCP实现QQ聊天
1.客户端参考代码 #coding=utf-8 from socket import * # 创建socket tcpClientSocket = socket(AF_INET, SOCK_STREA ...
- Android Stadio调试gradle 插件 || Android Stadio 远程调试 || Anroid APT调试
有时候,自己开发了gralde插件,想调试一下.毕竟打印log 成本太高.效率太低.怎么做呢? 第一种方法: 1.执行gradlew 命令的时候,加上几个参数:-Dorg.gradle.debug=t ...
- Unity3d工具方法小集
1.获取垂直水平方向上的输入: float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = I ...