[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 ...
随机推荐
- 电子商城实录------定义init初始化的方法
路由方法的设置 //路由方法 private static function dispatch(){ //获取控制器名称(类比:英文单词的后缀) $controller_name=CONTROLLER ...
- 利用phpspreadsheet切割excel大文件
背景: 利用phpspreadsheet可以轻松的解析excel文件,但是phpspreadsheet的内存消耗也是比较大的,我试过解析将近5M的纯文字excel内存使用量就会超过php默认的最大内存 ...
- rails中如何在a标签中添加其他标签
最近在用rails写一个项目练练手,然后遇到了一个问题,就是用 <% link_to("首页", root_path) %> 生成一个a标签,之后就在想我怎么在这个a标 ...
- 【数据结构】 List 简单实现
public class XList<T> : IEnumerable, IEnumerator { #region List 简单实现 /// <summary> /// 存 ...
- 阅读MDN文档之布局(四)
Introducing positioning Static positioning Relative positioning Introducing top, bottom, left and ri ...
- Selenium搭配TestNG
用Maven来构建TestNG依赖: <dependency> <groupId>org.testng</groupId> <artifactId>te ...
- K8s集群内热改代码
1.登录到k8s master服务器 $ ssh developer@XXX.XXX.X.XXX(IP地址) 2.查看服务容器所在的节点(以xx-server为例) $ kubectl get pod ...
- react错误总结
react-native 错误总结 The development server returned response error code: 500 in react-native https://b ...
- URAL 1732 Ministry of Truth(KMP)
Description In whiteblack on blackwhite is written the utterance that has been censored by the Minis ...
- android自定义View绘制圆形头像与椭圆头像
要实现这两种效果,需要自定义View,并且有两种实现方式. 第一种: public class BitmapShaders extends View { private BitmapSh ...