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. MVC CheckBoxList的实现

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...

  2. inline-block元素vertical-align的问题分析

    先来解释下这两个标签 inline-block: 字面意思:行内块元素,相当于强制转换,把一个标签设置为行内的块元素,既有块元素的部分特性(支持width\height\maigin-top\marg ...

  3. MongoDB【第二篇】MongoDB逻辑与物理存储结构

    基本的操作 一.常用的命令和基础知识 1.进入MongoDB sehll 首先我们进入到MongoDB所在目录执行 cd /work/app/mongodb/bin/ #启动 ./mongo 为了方便 ...

  4. 初识pipeline

    1.pipeline的产生 从一个现象说起,有一家咖啡吧生意特别好,每天来的客人络绎不绝,客人A来到柜台,客人B紧随其后,客人C排在客人B后面,客人D排在客人C后面,客人E排在客人D后面,一直排到店面 ...

  5. document.compatMode属性

    document.compatMode用来判断当前浏览器采用的渲染方式. 官方解释: BackCompat:标准兼容模式关闭.CSS1Compat:标准兼容模式开启. 当document.compat ...

  6. mvc添加多条数据到数据库

    from : http://www.th7.cn/Program/net/201502/387849.shtml mvc的视图太强大了,个人刚刚接触.(初级菜鸟,懂的不多,往大神们指点)需求是,客户点 ...

  7. Linux htop工具使用详解

    一.Htop的使用简介 大家可能对top监控软件比较熟悉,今天我为大家介绍另外一个监控软件Htop,姑且称之为top的增强版,相比top其有着很多自身的优势.如下: 两者相比起来,top比较繁琐 默认 ...

  8. web自动化工具-Browsersync

    web自动化工具-Browsersync browser-sync才是神器中的神器,和livereload一样支持监听所有文件.可是和livereload简单粗暴的F5刷新相比,browsersync ...

  9. 如何让两个div在同一行显示?一个float搞定

    最近在学习div和css,遇到了一些问题也解决了很多以前以为很难搞定的问题.比如:如何让两个div显示在同一行呢?(不是用table表格,table对SE不太友好)其实,<div> 是一个 ...

  10. 【Unity3d】火炬之光的X射线效果

    http://www.cnblogs.com/dosomething/archive/2012/04/03/2431149.html 人物和摄像机之间存在例如墙壁等的阻挡时  可以修改阻挡材质的alp ...