给定结点数n,结点值为1,2,...,n,求由这些结点可以构成的所有二叉查找树。

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

思路:递归构造,分别构造出左,右子树,然后组合成来。

/**
* 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)
{
return generateTrees(1,n);
} vector<TreeNode *> generateTrees(int start, int end)
{
vector<TreeNode *> trees;
if (start > end)
{
trees.push_back(NULL);
return trees;
}
if (start==end)
{
trees.push_back(new TreeNode(start));
return trees;
} for (int i=start; i<=end; ++i)
{
vector<TreeNode *> treesleft = generateTrees(start,i-1);
vector<TreeNode *> treesright = generateTrees(i+1,end); for (size_t j=0; j<treesleft.size(); ++j)
{
for (size_t k=0; k<treesright.size(); ++k)
{
TreeNode *root = new TreeNode(i);
root->left = treesleft[j];
root->right = treesright[k];
trees.push_back(root);
}
}
} return trees;
}
};

【Leetcod】Unique Binary Search Trees II的更多相关文章

  1. 【leetcode】Unique Binary Search Trees II

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

  2. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...

  3. 【leetcode】 Unique Binary Search Trees II (middle)☆

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

  4. 【树】Unique Binary Search Trees II

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

  5. 【Leetcode】【Medium】Unique Binary Search Trees II

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

  6. 【leetcode】Unique Binary Search Trees

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

  7. 【leetcode刷题笔记】Unique Binary Search Trees II

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

  8. 【leetcode】Unique Binary Search Trees (#96)

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

  9. 【题解】【BST】【Leetcode】Unique Binary Search Trees

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

随机推荐

  1. 卡特兰数(Catalan)简介

    Catalan序列是一个整数序列,其通项公式是 h(n)=C(2n,n)/(n+1) (n=0,1,2,...) 其前几项为 : 1, 1, 2, 5, 14, 42, 132, 429, 1430, ...

  2. SSH 配置日记

    1   注意struts2-spring-plugin.jar的导入.    Unable to load configuration. - action  异常.需要导入这个包 2  很久都跑不通的 ...

  3. poj 3630 Phone List(字典树)

    题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...

  4. 取代奶瓶Minidwep-gtk 破 WPA 全攻略

     目录 1. CDlinux 下使用 minidwepgtk 获取握手包并使用自带的字典破解 2. 自带的字典破解不出密码时使用 U 盘外挂字典继续暴力破解密码 3. 将握手包拷贝到 Windows ...

  5. paip.最新的c++ qt5.1.1环境搭建跟hello world

    paip.最新的c++ qt5.1.1环境搭建跟hello world 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://bl ...

  6. Android SDK三种更新失败及其解决方法

    更新Android SDK,居然失败了三次. 1.第一次失败 出现Failed to fetch URL错误提示 Failed to fetch URL https://dl-ssl.google.c ...

  7. objective-C学习笔记(二)类 class 和 结构 struct

    Objective-C的类型 引用类型 类 class 指针 pointer 块 block 值类型 基础数值类型 结构 struct 枚举 enum 类型装饰 协议 protocol 类别 cate ...

  8. commons-logging \ log4j \ slf4j 之间的关系

    最近的一个web项目中要使用到日志,但是对常用的日志记录工具(框架)着实不是很理解,在此mark一下. 1.commons-logging.jar common-logging是apache提供的一个 ...

  9. Protel对话窗字体显示不完全问题解决办法(PCB)

    点击protel99左上角的大箭头,点击preferences ,在对话框中把use client system fonts for all dialogs 复选框去掉,就可以了.

  10. Qt mac QMYSQL(动态加载插件QPluginLoader的用法)

    用此段代码查出问题所在 QPluginLoader loader; loader.setFileName("/Users/danny/Qt5.3.2/5.3/clang_64/plugins ...