2 Unique Binary Search Trees II_Leetcode
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
This is the second time I solve this problem.
Everytime when we encounter a BST problem without quite clear thought, we can resort to Divide & Conquer.
Use recursion to build the left and right subtree, then combine them then return.
In this problem, the left and right subtree can be multiple.
FIRST TRY ERROR: Forget to clear the vector of left of right subtree while apply different root.
Code:
/**
* 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) {
vector<TreeNode *> res;
if(n == 0)
{
res.push_back(NULL);
return res;
} res = gen(1, n);
return res;
} vector<TreeNode *> gen(int start, int end)
{
vector<TreeNode*> res;
if(start == end)
{
TreeNode* tmp = new TreeNode(start);
res.push_back(tmp);
return res;
} vector<TreeNode*> leftsub, rightsub;
for(int i = start; i <= end; i++)
{
leftsub.clear(); // First try error
rightsub.clear(); // First try error if(i == start) leftsub.push_back(NULL);
else leftsub = gen(start, i-1); if(i == end) rightsub.push_back(NULL);
else rightsub = gen(i+1, end); for(int m = 0; m < leftsub.size(); m++)
{
for(int n = 0; n < rightsub.size(); n++)
{
TreeNode* root = new TreeNode(i); // divide & conquer
root->left = leftsub[m];
root->right = rightsub[n];
res.push_back(root);
}
}
} return res;
}
};
2 Unique Binary Search Trees II_Leetcode的更多相关文章
- [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】95. 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
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 41. 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 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 I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
随机推荐
- List接口方法使用(PS:Java 编程思想阅读小结)
1.用代码说话 package JavaProject; import java.util.*; public class A{ public static void main(String[]arg ...
- C语言基础(9)-字符串格式化输入和输出
1.字符串在计算机内部的存储方式 字符串是内存中一段连续的char空间,以’\0’结尾 2.printf函数,putchar函数 putchar输出一个char printf是输出一个字符串 prin ...
- spring jdbc 查询结果返回对象、对象列表
首先,需要了解spring jdbc查询时,有三种回调方式来处理查询的结果集.可以参考 使用spring的JdbcTemplate进行查询的三种回调方式的比较,写得还不错. 1.返回对象(queryF ...
- tomcat启动的了,但是加载项目失败
解决方法: 1.tomcat启动是好的,也有可能找不到tomcat的dll,所以,检查一下myeclipse所使用的tomcat的解压目录是不是有空格,有空格的话,重新解压到一个新目录,千万不要有空格 ...
- mfc+vtk
MFC中view类主要处理显示视图,doc类处理文档,mainframe主要为整个窗口的和工程的设置管理.由此,VTK与MFC联合编程时,需要主要的是数据操作,以及显示要很好的与MFC中的结构结合,做 ...
- VB操作EXCEL文件
用VB操作Excel(VB6.0)(整理) 首先创建Excel对象,使用ComObj:Dim ExcelID as Excel.ApplicationSet ExcelID as new Excel. ...
- iOS的一像素线
文/stark_yang(简书作者)原文链接:http://www.jianshu.com/p/b83dca88ef73著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 时常总结以前学过 ...
- Sublime Text3插件管理
插件安装 package control 安装Sublime Text3 打开Sublime Text3,Ctrl+~ 调出控制台,输入代码安装 package control 代码如下: impor ...
- php中计算二维数组中某一元素之和
[0] => array(5) { ["id"] => string(2) "11" ["name"] => string ...
- BZOJ 2086: [Poi2010]Blocks
Description 每次可以将大于 \(k\) 的一个数 \(-1\), 在左边或右边的数 \(+1\) ,问最大能得到多长的序列每个数都大于等于 \(k\) . Sol 单调栈. 这道题好神啊q ...