Unique Binary Search Trees II

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

由于1~n是升序列,因此建起来的树天然就是BST。

递归思想,依次选择根节点,对左右子序列再分别建树。

由于左右子序列建树的结果也可能不止一种,需要考虑所有搭配情况。

vector<TreeNode *> left代表所有valid左子树。

vector<TreeNode *> right代表所有valid右子树。

/**
* 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 Helper(, n);
}
vector<TreeNode *> Helper(int begin, int end)
{
vector<TreeNode *> ret;
if(begin > end)
ret.push_back(NULL);
else if(begin == end)
{
TreeNode* node = new TreeNode(begin);
ret.push_back(node);
}
else
{
for(int i = begin; i <= end; i ++)
{//root
vector<TreeNode *> left = Helper(begin, i-);
vector<TreeNode *> right = Helper(i+, end);
for(int l = ; l < left.size(); l ++)
{
for(int r = ; r < right.size(); r ++)
{
//new tree
TreeNode* root = new TreeNode(i);
root->left = left[l];
root->right = right[r];
ret.push_back(root);
}
}
}
}
return ret;
}
};

【LeetCode】95. Unique Binary Search Trees II的更多相关文章

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

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

  2. 【一天一道LeetCode】#95. Unique Binary Search Trees II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LeetCode】96. Unique Binary Search Trees (2 solutions)

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

  4. 【LeetCode】96. Unique Binary Search Trees 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化递归 动态规划 卡特兰数 日期 题目地址:ht ...

  5. 【LeetCode】96 - Unique Binary Search Trees

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

  6. LeetCode OJ 95. Unique Binary Search Trees II

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

  7. [leetcode tree]95. Unique Binary Search Trees II

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

  8. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

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

随机推荐

  1. 14、SEO工程师要阅读的书籍 - IT软件人员书籍系列文章

    SEO工程师是Web项目中比较重要的一个角色.他主要负责网站的针对搜索引擎的优化方案的编写和实施.因为现在网站数量庞大,在全世界的这么多网站当中,想要让用户访问你的网站,就需要一些技巧性的内容.很多用 ...

  2. MySQL 5.6 主从复制如何处理——触发器,函数,存储过程,调度事件

      截图来自MySQL5.6的pdf版文档. 说明: 1)基于语句的复制时,trigger会在slave上执行,所以slave上也需要有trigger的定义,不然会导致主从数据不一致的: 2)基于行的 ...

  3. PostgreSql常用脚本

    添加表字段 ALTER TABLE public.university ADD COLUMN "Province" character varying(10); COMMENT O ...

  4. php数组高级小结(一)

    <?php /** * php5.4新增数组定义 */ $items1 = [ 'a','b','c' ]; $items2=[ 'name'=>'andy','age'=>52 ] ...

  5. JavaScript中变量提升是语言设计缺陷

    首先纠正下,文章标题里的 “变量提升” 名词是随大流叫法,“变量提升” 改为 “标识符提升” 更准确.因为变量一般指使用 var 声明的标识符,JS 里使用 function 声明的标识符也存在提升( ...

  6. 虚拟机centos6.5 --开放端口

    系统:centos6.5 1.查看端口开放情况 /etc/init.d/iptables status 2.开启端口 /sbin/iptables -I INPUT -p tcp --dport -j ...

  7. ganglia安装简记

    首先需要安装EPEL的源. yum install -y ganglia.x86_64 ganglia-gmetad.x86_64 ganglia-web.x86_64 ganglia-gmond.x ...

  8. 网格测地线算法(Geodesics in Heat)附源码

    测地线又称为大地线,可以定义为空间曲面上两点的局部最短路径.测地线具有广泛的应用,例如在工业上测地线最短的性质就意味着最优最省,在航海和航空中,轮船和飞机的运行路线就是测地线.[Crane et al ...

  9. KSFramework配置表:扩展表格解析类型

    解析和扩展表格 配置表示例 配置表模块在编译时,把Excel转化成TSV,并根据Excel的头部信息,生成对应的代码: 比如源码库中的Test.xlsx Excel文件,两个列头,Id和Value,其 ...

  10. 批处理文件指定jre路径启动java桌面应用程序

    应用场景: 我开发了一个应用程序,并连同jre一起刻成光盘,提供给用户,用户直接双击批处理文件即可运行,而不需要自己额外装jre. 目录组织结构如下: client |-images |-jre |- ...