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. 室内定位系列(三)——位置指纹法的实现(KNN)

    位置指纹法中最常用的算法是k最近邻(kNN):选取与当前RSS最邻近的k个指纹的位置估计当前位置,简单直观有效.本文介绍kNN用于定位的基本原理与具体实现(matlab.python). 基本原理 位 ...

  2. 【UWP】解析GB2312、GBK编码网页乱码问题

    在WebHttpRequest请求网页后,获取到的中文是乱码,类似这样: <title>˹ŵ��Ϸ���������� - ��̳������ -  ˹ŵ��Ϸ����</title ...

  3. ORACLE 常见错误

    ora-00904 :   标识符无效:查询语句中的列或表在oracle 中不存在:

  4. sql表分区

    1.单表达多少条数据后需要分区呢?   a.个人认为要似情况而定,有些常操作的表,分区反而带来麻烦,可以采用物理分表以及其它方法处理:   b.对于一些日志.历史订单类的查询数据,500w左右即可享受 ...

  5. 使用stack的思考

    对于使用stack进行()的配对检查,不需要使用额外的空间对每个字符进行存储和push与pop的操作. 只使用size对()进行处理即可,因为只有一种括号,所以入栈为size加1,出栈为size-1. ...

  6. js传入参数为字符串问题

    示例: var device_mac="11qweq234ert"; //第一种方式会报错:Onclick SyntaxError: identifier starts immed ...

  7. sqlservcer行列互转

    普通行列转换 行转列 假设有张学生成绩表(tb)如下:Name Subject Result张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94*/---- ...

  8. SQL分页获取数据

    SQL Server分页 select * from (') t Oracle分页 SELECT * FROM (' ORDER BY MaterialNM) t

  9. PYTHON map()函数详解

    map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 栗子如下↓ 图片来自于网上 def f(x,a): return x+x ...

  10. DBCP连接池配置示例

    <bean id="dataSourceOracle2" class="org.apache.commons.dbcp.BasicDataSource" ...