题目:Unique Binary Search TreesII

如果要列出所有可能的二叉搜索树,可以在上面的思路上进一步。

f(n) = f(0)*f(n-1) + f(1)*f(n-2) + ... + f(n-1)*f(0);

只要求出不同变量下的子树的所有情况,在整合到一起就可以了。

具体思路:

1.外循环遍历树根可能数值k(m->n);

2.分别求左右子树,左子树的可能取值范围(m->k-1),右子树的可能取值范围(k+1->n);

  注意左右子树可能为空,此时后面合并的时候要分开考虑,因为合并的时候是双重循环,外循环可能为空导致内循环的数据没有机会遍历;

3.最后整合,以当前值k为树根,把左右子树加进去。但是若左子树是l种情况,右子树是r种情况,一共是l*r种情况。

4.以上整个过程用递归描述,递归以当前给的范围来做树根。退出条件是范围内仅有一个可能数值,将它做树根直接返回。

注意:

1.n==0的情况单独考虑

2.左右子树可能为空。

 1 vector<TreeNode*> generateTrees(int n){
2 vector<TreeNode *>trees;
3 if (!n){//n==0时,空树
4 trees.push_back(NULL);
5 return trees;
6 }
7 pair<int, int> border(1,n);
8 generateTreeNum(trees,border);
9 return trees;
10 }
11
12 void generateTreeNum(vector<TreeNode *> &trees, pair<int, int>border){
13 if (border.first == border.second){
14 TreeNode *root = new TreeNode(border.first);
15 trees.push_back(root);
16 return;
17 }
18 for (int i = border.first; i <= border.second; i++)
19 {
20 vector<TreeNode *> lchild;
21 if (i != border.first){//递归求左子树
22 pair<int, int> p(border.first,i - 1);
23 generateTreeNum(lchild, p);
24 }
25 vector<TreeNode *> rchild;
26 if (i != border.second){//递归求右子树
27 pair<int, int> p(i + 1, border.second);
28 generateTreeNum(rchild, p);
29 }
30 if (!lchild.size()){//左子树为空,树根必定为border.first
31 vector<TreeNode *>::iterator it = rchild.begin();
32 while (it != rchild.end()){
33 TreeNode *root = new TreeNode(border.first);
34 root->right = (*it);
35 trees.push_back(root);
36 ++it;
37 }
38 }
39 else if (!rchild.size()){//右子树为空,树根必定为border.second
40 vector<TreeNode *>::iterator it = lchild.begin();
41 while (it != lchild.end()){
42 TreeNode *root = new TreeNode(border.second);
43 root->left = (*it);
44 trees.push_back(root);
45 ++it;
46 }
47 }
48 else{
49 vector<TreeNode *>::iterator lit = lchild.begin();
50 vector<TreeNode *>::iterator rit = rchild.begin();
51 while (lit != lchild.end()){
52 TreeNode *root = new TreeNode(i);
53 root->left = (*lit);
54 root->right = (*rit);
55 trees.push_back(root);
56 ++rit;//内循环递增右子树的情况
57 if (rit == rchild.end()){
58 ++lit;//外循环递增左子树的情况
59 rit = rchild.begin();
60 }
61 }
62 }
63 }
64 }

[LeetCode]Unique Binary Search TreesII的更多相关文章

  1. LeetCode:Unique Binary Search Trees I II

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

  2. Leetcode: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 ...

  3. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

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

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

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

  6. LeetCode - Unique Binary Search Trees II

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

  7. [leetcode]Unique Binary Search Trees @ Python

    原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...

  8. LEETCODE —— Unique Binary Search Trees [动态规划]

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

  9. Leetcode Unique Binary Search Trees

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

随机推荐

  1. sea.js的同步魔法

    前些时间也是想写点关于CMD模块规范的文字,以便帮助自己理解.今天看到一篇知乎回答,算是给了我一点启发. 同步写法却不阻塞? 先上一个sea.js很经典的模块写法: // 定义一个模块 define( ...

  2. 导入 SQL 时出现 Invalid default value for 'create_time' 报错解决方法

    问题描述 十三在 GitHub 仓库中开源了一个 Spring Boot 技术栈开发的 My-Blog 项目: 因为功能比较多,数据的存储就选择了 MySQL 数据库,该项目的表结构也放到了仓库中,方 ...

  3. PythonI/O进阶学习笔记_1.抽象、面向对象、class/object/type

    前言: 是自己在学习python进阶IO学习视频的时候的理解和笔记,因为很多都是本菜鸟学习时候的自己的理解,有可能理解有误. Content: - 抽象的概念和面向对象的概念?想要大概了解python ...

  4. Mybatis的一级缓存和二级缓存的理解以及用法

    程序中为什么使用缓存? 先了解一下缓存的概念:原始意义是指访问速度比一般随机存取存储器快的一种RAM,通常它不像系统主存那样使用DRAM技术,而使用昂贵但较快速的SRAM技术.对于我们编程来说,所谓的 ...

  5. python学习——文件操作

    打开文件 f = open(文件名, 文件打开模式,文件编码) ‘w’:只写模式,它是只能写,而不能读的.如果用’w’模式打开一个不存在的文件,则会创建新的文件开始写入:如果用’w’模式打开一个已存在 ...

  6. go语言实现分布式对象存储系统之单体对象存储

    对象存储 基本概念 主流存储类型分为三种:块存储.文件存储以及对象存储 NAS(文件存储):Network Attached storage,提供了存储功能和文件系统的网络服务器,客户端可以访问NAS ...

  7. UVA 10699 Count the factors 题解

    Time limit 3000 ms OS Linux Write a program, that computes the number of different prime factors in ...

  8. JAVA 泛型中的通配符 T,E,K,V,?

    前言 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许开发者在编译时检测到非法的类型. 泛型的本质是参数化类型,也就是说所操作的数据 ...

  9. 2019nc#4

    题号 标题 已通过代码 题解 通过率 团队的状态 A meeting 点击查看 树直径 604/2055   B xor 点击查看 线段树维护线性基交 81/861 未通过 C sequence 点击 ...

  10. lightoj 1126 - Building Twin Towers(dp,递推)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1126 题解:一道基础的dp就是简单的递推可以设dp[height_left][ ...