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

思路:通过递归实现。我们设置一个生成节点编号st到ed的子树的递归函数,对于给定的st到ed,我们枚举中间的每一个数当作root节点,假设数为i,则继续调用参数为st到i-1以及i+1到ed的该函数生成所有左孩子和右孩子子树。

 /**
* Definition for a binary tree node.
* 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 gen_sub(, n);
}
vector<TreeNode*> gen_sub(int st, int ed)
{
vector<TreeNode*> res;
if (st > ed)
res.push_back(NULL);
for (int i = st; i <= ed; i++)
{
vector<TreeNode*> lnodes = gen_sub(st, i - );
vector<TreeNode*> rnodes = gen_sub(i + , ed);
for (TreeNode *ln : lnodes)
for (TreeNode *rn : rnodes)
{
TreeNode *root = new TreeNode(i);
root->left = ln;
root->right = rn;
res.push_back(root);
}
}
return res;
}
};

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

  1. Unique Binary Search Trees II leetcode java

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

  2. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  3. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  4. 【LeetCode】95. 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

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

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

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

  7. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

  8. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

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

随机推荐

  1. vrpie下实现vrp模型和javascript的交互

    最近在做一个vrpie的项目,用vrp建模生成vrpie,然后在网页上面显示,这里需要和网页上面的其他内容交互,现在总结一下开发经验. 第一个需求是在网页上面点击那个的时候做一些事情,通过查找sdk找 ...

  2. python学习-- Django进阶之路 model的 objects对象 转 json

    # objects_to_json: 将 model对象 转化成 json# json_to_objects: 将 将反序列化的json 转为 model 对象 def json_field(fiel ...

  3. 12 JVM 垃圾回收(下)

    Java 虚拟机的堆划分 Java 虚拟机将堆划分为新生代和老年代.其中新生代又被划分为 Eden 区,以及两个大小相同的 Survivor 区. 默认情况下,Java 虚拟机采取一种动态分配的策略, ...

  4. Matlab freqs 函数

    freqs 模拟滤波器的频率响应 语法: h = freqs(b,a,w)[h,w] = freqs(b,a)[h,w] = freqs(b,a,f)freqs(b,a) 描述: freqs 返回一个 ...

  5. [oldboy-django][2深入django]xss攻击 + csrf

    1 xss攻击 xss攻击(跨站脚本攻击,用户页面提交数据来盗取cookie) - 慎用safe, 和mark_safe -- 如果要用,必须要过滤 - 定义: 用户提交内容,在页面展示用html显示 ...

  6. allocator class

    当分配一大块内存时,我们通常计划在这块内存上按需构造对象,这样的我们希望将内存分配和对象构造分离.但是通常的new关键字的分配的动态空间,有时候会造成一些浪费,更致命的是“如果一个类没有默认构造函数, ...

  7. 基于 Spring 和 iBATIS 的动态可更新多数据源持久层

    前言 我们时常会遇到一些 web 项目,需要从不同的数据源中抓取数据来进行分析,而这些数据源是有可能变化的,需要用户来进行动态的维护和添加.可是,大多数的 web 程序使用了应用服务器或者容器中间件来 ...

  8. hdu6212[区间dp] 2017青岛ACM-ICPC网络赛

    原题: BZOJ1032 (原题数据有问题) /*hdu6212[区间dp] 2017青岛ACM-ICPC网络赛*/ #include <bits/stdc++.h> using name ...

  9. 一个 Observation

    $n$ 个小球分布在一个圆上,小球的颜色或黑或白.顺时针(或逆时针)遍历这 $n$ 个小球,记录下相邻两小球的颜色,得到 $n$ 个有序颜色对.我们有,(黑,白)和(白,黑)的数目一定相等(可能都是 ...

  10. Educational Codeforces Round 22 E. Army Creation(分块好题)

    E. Army Creation time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...