[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 ...
随机推荐
- Spring IoC的底层技术支持——Java反射机制
我们知道,通过 new XmlClassPathApplicationContext("beans.xml")等方式即可启动容器.在容器启动时,Spring 根据配置文件的描述信息 ...
- ubuntu 18 lnmp
1安装Nginx sudo apt-get install nginx 2安装PHP sudo apt- php7.-fpm 3安装mysql sudo apt-get install mysql 启 ...
- Python正则表达式-基础
Python正则表达式-基础 本文转载自昔日暖阳,原文地址:http://www.osheep.cn/4806.html python使用正则,需要先引入re模块 import re 匹配符 单个字符 ...
- Kubernetes-apiserver
Kubernetes API服务器为API对象验证和配置数据,这些对象包含Pod.Service.ReplicationController等等.API Server提供REST操作以及前端到集群的共 ...
- babel配置
首页 首页 首页 博客园 博客园 博客园 联系我 联系我 联系我 demo demo demo GitHub GitHub GitHub 管理 管理 管理 魔魔魔芋芋芋铃铃铃 [02]websto ...
- 『Golang』在Golang中使用json
由于要开发一个小型的web应用,而web应用大部分都会使用json作为数据传输的格式,所以有了这篇文章. 包引用 import ( "encoding/json" "gi ...
- Fiddler安卓抓包详细教程
电脑端抓包一般图方便就用浏览器自带的,最近需要分析安卓一个APP的HTTP请求,尝试了wireshark(功能太强大了,然而我并不会用),tcpdump(用起来还是比较麻烦),网上搜了一下,还是使用F ...
- Spotlight on MySQL
聚光灯在MySQL 1.Sessios会话Total Users:总用户数前连接到MySQL服务器的用户会话总数Active Users:活跃用户此控件表示连接到当前正在执行SQL语句或其他数据库请求 ...
- 第二十一篇 json,picklz,xml模块
Json模块 Json模块比较简单,仅有四个方法dumps()和loads()方法,dump()和load()方法,但是却非常的常用,实用性极强. 如果要在不同的编程语言之间传递对象,就必须把对象序列 ...
- 目标检测之Faster-RCNN的pytorch代码详解(数据预处理篇)
首先贴上代码原作者的github:https://github.com/chenyuntc/simple-faster-rcnn-pytorch(非代码作者,博文只解释代码) 今天看完了simple- ...