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.

    \       /     /      / \      \

    /     /       \                 \
3
[1] [4] [5] [3] [2]

=====================BST树,平衡查找树

如果将上图中的实例按照[1]..[5]的下标来摆放,

以1为根的树的个数,等于左子树的个数乘以右子树的个数,左子树是0个元素的树,右子树是2个元素的树.

以2为根的树的个数,等于左子树的个数乘以右子树的个数,左子树的元素个数为1,右子树元素个数为为1

...以此类推

========

当数组1,2,3...n时,基于一下原则构建BST树具有唯一性:以i为根节点的树,其左子树由[1,i-1]构成

其右子树由[i+1,n]构成.

定义f(i)为以int a[]=[1,i]能产生的Uniuqe BST的数目,

那么如果a[]为空,那么只有一种BST,即空树,f(0)=1

if a.size==1,单个节点,那么只有一种BST,f(1)=1

if a.size==2,有如下两种可能

1         2

\       /

2    1

f(2) = f(0)*f(1), 1为根的情况

     +f(1)*f(0), 2为根的情况

if a.size==3,那么

f(3) = f(0)*f(2)//root=1

     +f(1)*f(1)//root=2

      +f(2)*f(0)//root=3

可以发现f的递归公式为

==========

/**
* 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==) return generateTrees(,);
return generateTrees(,n);
} vector<TreeNode*> generateTrees(int start,int end){
vector<TreeNode*> subTree;
if(start>end){
subTree.push_back(nullptr);
return subTree;
} for(int k = start;k<=end;k++){
vector<TreeNode*> leftSubs = generateTrees(start,k-);
vector<TreeNode*> rightSubs = generateTrees(k+,end);
for(auto i:leftSubs){
for(auto j:rightSubs){
TreeNode *node = new TreeNode(k);
node->left = i;
node->right = j;
subTree.push_back(node);
}
}
}
return subTree;
}
};
  

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

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

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

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

  3. 【LeetCode】95. 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] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

  6. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

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

  7. [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II

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

  8. leetcode 95 Unique Binary Search Trees II ----- java

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

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

随机推荐

  1. 灰度图像的自动阈值分割(Otsu 法)(转载)

    灰度图像的自动阈值分割(Otsu 法) 机器视觉领域许多算法都要求先对图像进行二值化.这种二值化操作阈值的选取非常重要.阈值选取的不合适,可能得到的结果就毫无用处.今天就来讲讲一种自动计算阈值的方法. ...

  2. 《C标准库》—之<assert.h>实现

    首先,贴出标准库中<assert.h>的实现源码: #undef assert #ifdef NDEBUG #define assert(test)((void)0) #else void ...

  3. Fresco最最最简单使用

    http://blog.csdn.net/wa991830558/article/details/46005063

  4. hdu1712 ACboy needs your help 分组背包

    最基础的分组背包~ #include <iostream> #include <cstdio> #include <cstdlib> #include <cs ...

  5. UVALive-4670 Dominating Patterns(AC自动机)

    题目大意:找出出现次数最多的模式串. 题目分析:AC自动机裸题. 代码如下: # include<iostream> # include<cstdio> # include&l ...

  6. Python文件格式化写入

    [root@localhost test]# cat 1.py fd = open('format.txt','w') head = "%10s%10s%10s\n"%('id', ...

  7. (转)The Road to TensorFlow

    Stephen Smith's Blog All things Sage 300… The Road to TensorFlow – Part 7: Finally Some Code leave a ...

  8. 在windows系统上安装caffe

    下载编译 0.确认电脑上有VS2013 0.确认显卡GPU Compute Capability>=3.0 1.安装CUDA7.5 2.下载cuDNN v4,添加到CUDA7.5 3.根据htt ...

  9. Python的numpy库下的几个小函数的用法

    numpy库是Python进行数据分析和矩阵运算的一个非常重要的库,可以说numpy让Python有了matlab的味道 本文主要介绍几个numpy库下的小函数. 1.mat函数 mat函数可以将目标 ...

  10. 利用zip(或者phar)协议进行本地文件包含

    $include_file=$_GET[include_file];if ( isset( $include_file ) && strtolower( substr( $includ ...