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的全部排列:

遍历1~n中的每一个数k,作为根结点,所有比k小的数放在左子树,比k大的数放在右子树;

则以k为根节点的1~n全排列 等于 k的左子树全排列 与 k的右子树全排列的组合;

由此使用递归的方法,得到所有结果。

注意,递归只需考虑三个情况:

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(, n);
} vector<TreeNode *> __generateTrees (int begin, int end) {
vector<TreeNode *> cur_nodes_mathods;
if (begin > end) {
cur_nodes_mathods.push_back(NULL);
return cur_nodes_mathods;
} for (int i = begin; i <= end; ++i) {
vector<TreeNode *> lefts = __generateTrees(begin, i - );
vector<TreeNode *> rights = __generateTrees(i + , end); for (int j = ; j < lefts.size(); ++j) {
for (int k = ; k < rights.size(); ++k) {
TreeNode *cur_root = new TreeNode(i);
cur_root->left = lefts[j];
cur_root->right = rights[k];
cur_nodes_mathods.push_back(cur_root);
//delete cur_root;
}
}
} return cur_nodes_mathods;
}
};
												

【Leetcode】【Medium】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

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

  3. 【leetcode】Unique Binary Search Trees II

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

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

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

  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解题报告—— 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 ...

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

  8. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

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

  10. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...

随机推荐

  1. jmeter安装部署、maven路径配置

    jmeter下载地址: https://jmeter.apache.org/download_jmeter.cgi 解压文件 配置jmeter环境变量 (1)设置jmeter解压目录的JMETER_H ...

  2. cv2.FileNode has no keys

    把 python-opencv  版本由3.4.1 换成 3.4.4之后,问题解决

  3. Go语言小试牛刀---几个简单的例子

    整理资料,发现之前手写的Go语言资料,现在贴过来. 第一个:Channel的使用,创建一个随机数 package main import "fmt" import "ru ...

  4. Linux 中的rsh,ssh

    第一部分 rsh 的安装与配置 http://www.ttlsa.com/linux/configure-rsh-rcp-scp-on-centos/ http://www.ahlinux.com/s ...

  5. 2018年javaee学习目标

    我打算在本学期的学习中把java ee掌握并能熟练应用,如果可以的话还打算编写一个后台服务程序,增加自己的实战经验.

  6. springboot项目更改代码后实时刷新问题

    在spring boot使用的过程中, 发现我修改了静态文件, 前台刷新后, 没有任何变化, 必须重新启动, 才能看到, 这简直不能让人接受. 那有什么方法来解决这个问题呢. Baidu之后, 得到了 ...

  7. [shell基础]——变量

    变量的赋值 #定义变量,注意等号两边没有任何空格 variable=#定义环境变量export variable= #双引号:可含空格.可转义特殊字符 variable=" " # ...

  8. Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】

     Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. .net面试题升级版

    1.列举ASP.NET页面之间传值的几种方式. 答:使用QueryString,如.../id=1;response.Redirect() 使用Session 使用Server.Transfer 使用 ...

  10. java类与对象基础--抽象类和接口

    在java的类体系中,有两个比较特殊的抽象体--抽象类和接口.抽象体并不可以拥有具体的对象(而且接口甚至不是类),但是它们却是在java设计领域非常重要的两个概念,很多优秀的设计模式都是基于这两个概念 ...