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的更多相关文章

  1. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  2. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  3. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  4. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  5. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  6. 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 ...

  7. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  8. LeetCode - Unique Binary Search Trees II

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  9. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

随机推荐

  1. JAVA中ListIterator和Iterator详解与辨析

    在使用Java集 合的时候,都需要使用Iterator.但是java集合中还有一个迭代器ListIterator,在使用List.ArrayList. LinkedList和Vector的时候可以使用 ...

  2. 《你不常用的c#之XX》

    你不常用的c#之一>:略谈unsafe http://blog.csdn.net/robingaoxb/article/details/6199508 <你不常用的c#之二>:略谈G ...

  3. jsp中超链接路径的写法

    主题 超链接不就是一个地址字符串吗?这能有什么花头? LZSB! 曾经我也是这么想的.... 最近对apache的学习让我对网页中超链接,CSS,js的路径的写法有了一些新的认识. 所以这篇文章主要分 ...

  4. runtime-给系统已有类添加属性

    在没有接触runtime之前,我们接触到的能给类进行扩展的方法有类目(category)和延展(extension)两种.类目(category)可以给系统已有类添加扩展方法但是不能添加属性,并且被添 ...

  5. AngularJS学习第一课

    AngularJS2与AngularJS1有很大的不同. AngularJS2支持ECMAScript6,为了提升性能,支持移动化开发(安卓.ios平台),因此升级为AngularJS 2,Angul ...

  6. Myeclipse 构建工作空间出错

    MyEclipse工作空间报错如下:'Building workspace' has encountered a problem. Errors occurred during the build.并 ...

  7. JQuery+Ajax+Struts2+Hibernate 实现完整的登录注册

    写在最前: 下午有招聘会,不想去,总觉得没有准备好,而且都是一些不对口的公司,可是又静不下心来,就来写个博客. 最近在仿造一个书城的网站:http://www.yousuu.com ,UI直接拿来用, ...

  8. QuartusII Design partion and logic lock

    Design partion Design partion常用于“增益变量(QIC)”,通过Design Partition对子模块进行“逻辑分区”,在Design Partition Window中 ...

  9. HTML和XHTML的区别

    引言:XHTML是用XML语言重写了HTML,也可以理解为XHTML是HTML的过渡语言,它比HTML语言更严谨,基本语言都还是沿用HTML的标签. XHTML和HTML的差别主要分为两大类: 功能上 ...

  10. jquery load 和 iframe 比较

    如果要加载的东西比较简单,里面的没有复杂的数据和逻辑,可以使用load.如果要加载的页面自身有复杂的逻辑.操作,还是建议使用ifame,因为iframe里面可以引入自身的js和样式,而load引入的东 ...