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. vim编辑器最简单使用方法

    i 输入模式 :q 不保存退出 :q! 强制退出 :wq 保存退出 j 下 k 上 h 左 l 右 gg start G end x 往后删 X 往前删 yy 复制行 p 粘贴 dd 剪切行 u 撤销 ...

  2. Django--源码安装

    1.安装setuptools cd /usr/src tar zxf setuptools-18.3.2.tar.gz cd setuptools-18.3.2/ python setup.py bu ...

  3. Go语言之并发编程(一)

    轻量级线程(goroutine) 在编写socket网络程序时,需要提前准备一个线程池为每一个socket的收发包分配一个线程.开发人员需要在线程数量和CPU数量间建立一个对应关系,以保证每个任务能及 ...

  4. 【Rotate List】cpp

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...

  5. C# 中的 #region 和 #endregion 的作用

    C#中的 #region 和 #endregion 表示一块区域,这样在 Visual Studio 中可以将这块区域的代码折叠起来,便于查看. 虽然Visual Studio 也响应大括号的折叠,但 ...

  6. [转]Docker容器内不能联网的6种解决方案

    注: 下面的方法是在容器内能ping通公网IP的解决方案,如果连公网IP都ping不通,那主机可能也上不了网(尝试ping 8.8.8.8) 1.使用--net:host选项 sudo docker ...

  7. 微信Oauth2.0网页开放授权

    网页授权获取用户基本信息 如果用户在微信中(Web微信除外)访问公众号的第三方网页,公众号开发者可以通过此接口获取当前用户基本信息(包括昵称.性别.城市.国家).利用用户信息,可以实现体验优化.用户来 ...

  8. IO Streams:数据流

    数据流支持原始数据类型值(布尔型,字符型,字节型,短型,长整型,浮点型和双倍型)的二进制I / O以及字符串值.所有数据流都实现了DataInput接口或DataOutput接口.本节重点介绍这些接口 ...

  9. topK问题解法

    topK问题的最佳解法是堆排,下面介绍用堆排来解决该问题. 堆排解决topK问题的思路,取出前K个数,最重要的就是要减少比较的次数,用堆排维护一个K大小的堆,比如一个小顶堆,则堆顶为堆中最小的值,将堆 ...

  10. linux删除大量文件

    1.建立一个空目录 mkdir -p /tmp/rsync_blank 2.确立需要清空的目标目录 /data/web/vip/htdocs/tuan 3.使用rsync同步删除(注意目录后面的“/” ...