[LeetCode]Unique Binary Search TreesII
题目:Unique Binary Search TreesII
如果要列出所有可能的二叉搜索树,可以在上面的思路上进一步。
f(n) = f(0)*f(n-1) + f(1)*f(n-2) + ... + f(n-1)*f(0);
只要求出不同变量下的子树的所有情况,在整合到一起就可以了。
具体思路:
1.外循环遍历树根可能数值k(m->n);
2.分别求左右子树,左子树的可能取值范围(m->k-1),右子树的可能取值范围(k+1->n);
注意左右子树可能为空,此时后面合并的时候要分开考虑,因为合并的时候是双重循环,外循环可能为空导致内循环的数据没有机会遍历;
3.最后整合,以当前值k为树根,把左右子树加进去。但是若左子树是l种情况,右子树是r种情况,一共是l*r种情况。
4.以上整个过程用递归描述,递归以当前给的范围来做树根。退出条件是范围内仅有一个可能数值,将它做树根直接返回。
注意:
1.n==0的情况单独考虑
2.左右子树可能为空。
1 vector<TreeNode*> generateTrees(int n){
2 vector<TreeNode *>trees;
3 if (!n){//n==0时,空树
4 trees.push_back(NULL);
5 return trees;
6 }
7 pair<int, int> border(1,n);
8 generateTreeNum(trees,border);
9 return trees;
10 }
11
12 void generateTreeNum(vector<TreeNode *> &trees, pair<int, int>border){
13 if (border.first == border.second){
14 TreeNode *root = new TreeNode(border.first);
15 trees.push_back(root);
16 return;
17 }
18 for (int i = border.first; i <= border.second; i++)
19 {
20 vector<TreeNode *> lchild;
21 if (i != border.first){//递归求左子树
22 pair<int, int> p(border.first,i - 1);
23 generateTreeNum(lchild, p);
24 }
25 vector<TreeNode *> rchild;
26 if (i != border.second){//递归求右子树
27 pair<int, int> p(i + 1, border.second);
28 generateTreeNum(rchild, p);
29 }
30 if (!lchild.size()){//左子树为空,树根必定为border.first
31 vector<TreeNode *>::iterator it = rchild.begin();
32 while (it != rchild.end()){
33 TreeNode *root = new TreeNode(border.first);
34 root->right = (*it);
35 trees.push_back(root);
36 ++it;
37 }
38 }
39 else if (!rchild.size()){//右子树为空,树根必定为border.second
40 vector<TreeNode *>::iterator it = lchild.begin();
41 while (it != lchild.end()){
42 TreeNode *root = new TreeNode(border.second);
43 root->left = (*it);
44 trees.push_back(root);
45 ++it;
46 }
47 }
48 else{
49 vector<TreeNode *>::iterator lit = lchild.begin();
50 vector<TreeNode *>::iterator rit = rchild.begin();
51 while (lit != lchild.end()){
52 TreeNode *root = new TreeNode(i);
53 root->left = (*lit);
54 root->right = (*rit);
55 trees.push_back(root);
56 ++rit;//内循环递增右子树的情况
57 if (rit == rchild.end()){
58 ++lit;//外循环递增左子树的情况
59 rit = rchild.begin();
60 }
61 }
62 }
63 }
64 }
[LeetCode]Unique Binary Search TreesII的更多相关文章
- 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 & Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- [LeetCode] 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 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 解题报告
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 @ Python
原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...
- LEETCODE —— 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
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- Linux--shell数组和字符串--09
一.数组 数组就是一段连续的变量,一段连续的内存存储空间,为了解决变量过多的问题,在同一类变量中,我们不需要去定义多个名字,而是以数组的方式来定义 1.定义数组 declare -a 定义数组 dec ...
- Mac查看及清理QQ、微信之前下载的图片、视频或DB等
之前写过一篇清理Mac空间的文章: Mac系统清理.占用空间大.空间不够.查看系统文件大小分布 其实这篇文章不是太全,有些资源还是清理不彻底,正好前段时间需要找微信下载的资源,其实可以算作空间清理的续 ...
- windows安装nginx、mysql等软件并加入系统服务启动详细
windows类系统安装nginx.mysql软件 (PS:windows系统环境中设置完nginx.mysql环境变量,需要重新启动系统才会生效.) 一.NGINX:首先下载windows版ngin ...
- (四十八)c#Winform自定义控件-下拉按钮
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- python学习之并发编程
目录 一.并发编程之多进程 1.multiprocessing模块介绍 2.Process类的介绍 3.Process类的使用 3.1 创建开启子进程的两种方式 3.2 获取进程pid 3.3验证进程 ...
- 如何编写高质量的 JS 函数(2) -- 命名/注释/鲁棒篇
本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/sd2oX0Z_cMY8_GvFg8pO4Q作者:杨昆 上篇<如何编写高质量的 JS 函数 ...
- NLP(十六) DL在NLP中的应用
深度学习中的核心主题是卷积神经网络(CNN)和循环神经网络(RNN) 卷积神经网络 CNN用于图像处理 卷积: 原始图像 5×5 滤波器 3×3 滤波器以步长大于小于1,到处平移,并与原始图像里的3× ...
- Problem : 这个题如果不是签到题 Asm.Def就女装(积性函数dp
https://oj.neu.edu.cn/problem/1460 思路:若n=(p1^a1)*(p2^a2)...(pn^an),则f(n,0)=a1*a2*...*an,显然f(n,0)是积性函 ...
- Timus-1005. Stone Pile-01背包
传送门:http://acm.timus.ru/problem.aspx?space=1&num=1005 参考:https://www.cnblogs.com/yinzm/p/6629222 ...
- 51nod 1218 最长递增子序列 V2(dp + 思维)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218 题解:先要确定这些点是不是属于最长递增序列然后再确定这 ...