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

分析:

递归即可。枚举所有的 root 节点,root 的左子树和右子树就是两个子问题,递归求解。

但是有一点要注意,容易出错。

如果子树的节点个数为零,这时候返回的应该是一个包含 NULL 的 vector;

/**
* 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;
}
return generateTrees(1, n);
}
private:
vector<TreeNode *> generateTrees(int left, int right)
{
TreeNode *node(NULL);
vector<TreeNode*> trees, left_tree, right_tree;
if(left > right)
{
trees.push_back(NULL);
return trees;
}
for(int i=left; i<=right; ++i)
{
left_tree = generateTrees(left, i-1);
right_tree = generateTrees(i+1, right);
for(auto j : left_tree)
for(auto k : right_tree)
{
node = new TreeNode(i);
node->left = j;
node->right = k;
trees.push_back(node);
}
}
return trees;
}
};

LeetCode----Unique Binary Search Trees 2的更多相关文章

  1. LeetCode:Unique Binary Search Trees I II

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

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

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

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

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

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

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

  5. LeetCode - Unique Binary Search Trees II

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

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

  7. [leetcode]Unique Binary Search Trees @ Python

    原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...

  8. LEETCODE —— Unique Binary Search Trees [动态规划]

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

  9. Leetcode Unique Binary Search Trees

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

  10. LeetCode: Unique Binary Search Trees [095]

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

随机推荐

  1. each用法

    1.数组用法 <script> var s=["s","i","l","e","n",& ...

  2. hdu----(3068)最长回文(manacher)

    最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. 如何在Objective-C中实现链式语法

    在接触到开源项目 Masonry 后,里面的布局约束的链式写法让我颇感兴趣,就像下面这样: 1 2 3 4 5 6 7 8 UIEdgeInsets padding = UIEdgeInsetsMak ...

  4. 用XmlSerializer进行xml反序列化的时候,程序报错: 不应有 <xml xmlns=''>

    原因 一,类型错误: 比如xml本来是UserInfo类型 用XmlSerializer进行反序列化传入的类型是MemberInfo这就会报错 二,xml根节点和对象的类名不一致,而又没有对类加入[X ...

  5. px和em的区别

    px和em的区别 2012-06-21 23:01:06|  分类: CSS|字号 订阅 在如今这个提倡可用性设计以及用户体验设计的网络时代,CSS也是要一同参与其中的.大部分人在CSS代码编写中总是 ...

  6. js为元素添加onclick事件

    $("div.manu a:last").on('click',function(){ if (page == totalPage) { return; } page = page ...

  7. BZOJ1520 [POI2006]Szk-Schools

    裸的费用流啊... 建图:对于一个点p拆成两个p1和p2,S向p1连边,流量为1,费用为0:p2向T连边流量为1,费用为0 然后i1向a2到b2分别连边,不妨设i1向p2连边,流量为1,费用为|i - ...

  8. display:inline-block;如何取消标签之间的距离

    <div style="font-size:0px"> <div style=" display:inline-block; zoom:1;*displ ...

  9. GFS: Evolution on Fast-forward

    GFS: Evolution on Fast-forward by Marshall Kirk McKusick, Sean Quinlan | August 7, 2009 A discussion ...

  10. 利用百度地图开源sdk获取地址信息。

    注册百度开发者帐号,下载相关sdk 添加权限: 添加百度注册访问应用(AK)码 添加源代码文件到libs文件: 代码如下: package com.lixu.baidu_gps; import com ...