Question

Given an integer 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

Solution

分治的思想,分别求出1~n个节点为根节点构成的BST.

例如 k, 1<= k <= n; 为根节点的问题可以划分为,左子树和右子树,左子树中的节点值为1k-1,然后1k-1分别为根节点,同理右子树。右子树中的节点值为k+1n,然后k+1n分别为根节点,以此类推。

Code

/**
* 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) {
if (n <= 0)
return vector<TreeNode*>();
else
return generateSubTree(1, n);
}
vector<TreeNode*> generateSubTree(int s, int e) {
vector<TreeNode*> res;
if (s > e) {
res.push_back(NULL);
return res;
}
for (int i = s; i <= e; i++) {
vector<TreeNode*> left = generateSubTree(s, i - 1);
vector<TreeNode*> right = generateSubTree(i + 1, e); // 所有组合 = size(left) * size(right)
for (TreeNode* p : left) {
for (TreeNode* q : right) {
struct TreeNode* root = new TreeNode(i);
root->left = p;
root->right = q;
res.push_back(root);
}
}
}
return res;
}
};

LeetCode——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 独一无二的二叉搜索树之二

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

  3. LeetCode - Unique Binary Search Trees II

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

  4. [Leetcode] Unique binary search trees ii 唯一二叉搜索树

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

  5. [LeetCode] Unique Binary Search Trees II dfs 深度搜索

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

  6. [leetcode]Unique Binary Search Trees II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上 ...

  7. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

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

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

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

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

随机推荐

  1. [iOS微博项目 - 3.5] - 封装业务

    github: https://github.com/hellovoidworld/HVWWeibo   A.封装微博业务 1.需求 把微博相关业务(读取.写微博) 界面控制器不需要知道微博操作细节( ...

  2. 如何让ios启动画面停留更长时间

    几种方法: 方法1:在AppDelegate.m里写上 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithO ...

  3. 【opencv】c++ 读取图片 & 绘制点 & 绘制文字 & 保存图片

    //read pic ]; sprintf(path, "%s%d/%s", image_dir.c_str(), cam_num, filename.c_str()); cv:: ...

  4. 找新朋友---hdu1286(欧拉函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1286 欧拉函数:对正整数n,欧拉函数是求少于n的数中与n互质的数的数目: 素数(质数)指在一个大于1的 ...

  5. 七牛云存储--内存put示例(go sdk)

    啥都不说了,居然有文档,有git为啥不提供example? 自己看代码,琢磨了一下,原来是要这么用的.这里不得不吐槽一下package的命名,为啥要去io?golang自带系统包名就有io啊,哥哥. ...

  6. POJ 3171

    题目大意:        给定一个区间范围[M,E],接下来有n行输入.每行输入三个数值:T1,T2,S,表示覆盖区间[T1,T2] 的代价为S,要求你求出覆盖区间[M,E]的最小代价,假设不能覆盖, ...

  7. PHP归档phar性能測试

    PHP自从5.3后新增PHAR归档,Phar 归档的概念来自 Java™ 技术的 JAR 归档,它同意使用单个文件打包应用程序.这个文件里包括运行应用程序所需的全部东西.该文件不同于单个可运行文件,后 ...

  8. abap编辑器中代码不可修改

    当出现abap编辑器中代码不能修改的情况,可以按如下设置: edit——> 修改操作——>关闭助手

  9. python学习笔记(二十五)重写父类方法

    python继承中,如果子类在调用某个方法时,它首先是从派生类(也就是当前类)中去找对应的方法,如果当前类中找不到对应的方法,就会去基类(派生类)中去逐个查找. 若父类的方法不能满足子类的需要,那么子 ...

  10. UVA10026:Shoemaker's Problem(贪心)

    题目链接:  http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/K 题目需求:鞋匠有n个任务,第i个任务要花费ti ...