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

转自: http://yucoding.blogspot.com/2013/05/leetcode-question115-unique-binary.html  

Analysis:

The basic idea is still using the DFS scheme. It is a little hard to think the structure of the argument list in the function. It is clear that for each tree/subtree, we will set the root as the start position to the end position, and recursively construct the left subtree with the left part and the right subtree with the right part.
So first we can have
 void dfs (int st, int ed     ){
   if (st>ed) {   // generate a null node }
  else{
    for (int i=st;i<=ed;i++){
        
      dfs(st,i-1,   );     //generate left subtree 
      dfs(i+1,ed,   );  // generate right subtree
    }
  }

}

Next step is to think about how to store all the possible solutions.
This is important ! Think about the root node, all the possible solutions of the tree are from the combinations of all the possible solutions of its left subtree, and its right subtree. One step further, if we have a root node and a left node, for the left node, still the subtrees below it are the combinations of the possible solutions of its left and right subtree, until the leaf node.

In other words, we store all the possible solutions for each node, instead of storing the only tree. So, we can have
void dfs(int st, int ed, vector<TreeNode *> res){}
in this function, recursively generate the left tree and right tree, then construct the current node, and push it to the current solution vector.
Details see the code.

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void dfs(vector<TreeNode *> &res, int left , int right){
if(left > right){
res.push_back(NULL);
return ;
} for(int k = left; k <= right ; k++)
{
vector<TreeNode *> leftTree;
dfs(leftTree, left, k-);
vector<TreeNode *> rightTree;
dfs(rightTree, k+, right);
for(int i = ; i < leftTree.size(); i++)
for( int j = ; j < rightTree.size(); j++)
{
TreeNode *root = new TreeNode(k);
root->left = leftTree[i];
root->right = rightTree[j];
res.push_back(root);
}
}
}
vector<TreeNode *> generateTrees(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<TreeNode *> res;
dfs(res, , n);
return res;
}
};

LeetCode_Unique Binary Search Trees II的更多相关文章

  1. 【LeetCode】95. 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

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

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

  4. LeetCode: Unique Binary Search Trees II 解题报告

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

  5. Unique Binary Search Trees,Unique Binary Search Trees II

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

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

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

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

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

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

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

随机推荐

  1. protel或Altium Designer中各种栅格(grid)的意义

    Snap Grid:捕获栅格.指光标移动的最小间隔.Component Grid:元件放置捕获栅格.指放置元件时,元件移动的间隔.Electrical Grid:电气栅格.电气栅格的作用是在移动或放置 ...

  2. CentOs6.5中安装和配置vsftp简明

    这篇文章主要介绍了CentOs6.5中安装和配置vsftp简明教程,需要的朋友可以参考下     一.vsftp安装篇 复制代码代码如下: # 安装vsftpdyum -y install vsftp ...

  3. using 1.7 requires using android build tools version 19 or later

    这意思大概是adt用了1.7,abt(android build tools)就要用19或更高,可是abt在哪设置呢,原来是在sdk manager中 之前我已安装的最高的abt是17,然后~~~,F ...

  4. eucMenu

  5. [置顶] android之存储篇_SQLite数据库_让你彻底学会SQLite的使用

    SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么. 例如:可以在Integer类型的字段中存放字符串,或者在布尔型字段中存放浮点数,或者在字符型字段中 ...

  6. c语言筛选质数

    #include <stdio.h> #include <stdlib.h> #include <math.h> int isit(int num) { int i ...

  7. html5标签收集

    <meta name="viewport" content="width=device-width,initial-scale=1 user-scalable=0& ...

  8. (转)直接保存对象的数据库——db4o

    在实际开发中,数据的存储是必不可少的,常用的有数据库存储和文件存储.数据库目前有关系型数据库和文档型数据库(No-SQL).关系型数据库以字段.类型.约束.表关系来存储和管理数据,比较常见的比如Ora ...

  9. [置顶] 【VB.NET2010】注册表写入方法

    这里,以添加启动项为例: Imports Microsoft.Win32 Imports System.Reflection.Assembly   Private Sub SetAuto_Click( ...

  10. vsftp虚拟用户登录配置详解

    一.安装:1.安装Vsftpd服务:# yum install vsftpd 2.安装DB4部件包:这里要特别安装一个db4的包,用来支持文件数据库.# yum install db4-utils 二 ...