给定结点数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. Tomcat+redis+nginx配置

    为客户开发的一个绩效系统,采用了java web的开发方式,使用了一些spring mvc, mybatis之类的框架.相比于oracle ebs的二次开发,这种开发更加灵活,虽然和ebs集成的时候遇 ...

  2. jvm学习小结

    1. JDK.JRE.JVM之间的关系.JDK包含JRE和其它开发工具库如编译器.调试期,jConsele性能检测工具等2. JVM的构成:类装载器子系统.执行引擎.运行时数据区,如下图: 3. JV ...

  3. Objective-c 访问控制

    在Objective-c中定义类时,也可以使用类似于C++中public.private来修饰成员变量,如下: @intterface Test:NSObject{ @public int i; in ...

  4. Js动态设置Img大小

    function ResizePic() {        $('img').each(function () {            var maxWidth = 450; // 图片最大宽度   ...

  5. URAL 1260 Nudnik Photographer DFS DP

    题目:click here :这个题可以先dfs深搜下,规律dp dfs: #include <bits/stdc++.h> using namespace std; #define S ...

  6. JBPM6教程

    JBPM6教程-手把手教你安装JBPM 1. 安装JBPM的先决条件: (1)JDK 1.6+以上,没有安装的话,猛击这里. (2)Ant 1.7+以上,没有安装的话,看看这里. 2. 下载JBPM安 ...

  7. 替换NavigationController里面的返回按钮

    通过navigationController push进来的controller,默认的返回按钮是将本controller pop出去. 但有时候想在pop出去前完成一些自己的一些事情,这时可以自己写 ...

  8. Django内置template标签

    html过滤{% autoescape on|off %} {{body}} {% endautoescape %} 注释{% comment %} {% endcomment %} csrf攻击 { ...

  9. QTcpSocket通信编程时阻塞与非阻塞的问题

    目标,qt程序作为客户端,windows下winsock作为服务器端,实现两端通信. 开始时写了一个小函数测试: [cpp] view plaincopy QTcpSocket tmpSock;  t ...

  10. Qt探秘——谈ui文件的用法

    转载自:点击打开链接http://blog.csdn.net/luo_isaiah/article/details/5794973 相信用过Qt Designer的朋友,对Qt Project中的.u ...