题目:

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

代码:

/**
* 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) {
return Solution::generateBST(, n);
}
static vector<TreeNode*> generateBST(int min, int max)
{
vector<TreeNode*> ret;
if ( min>max ) { ret.push_back(NULL); return ret; }
for ( int i = min; i<=max; ++i )
{
vector<TreeNode*> left = Solution::generateBST(min, i-);
vector<TreeNode*> right = Solution::generateBST(i+,max);
for ( size_t l = ; l < left.size(); ++l )
{
for ( size_t r = ; r < right.size(); ++r )
{
TreeNode *root = new TreeNode(i);
root->left = left[l];
root->right = right[r];
ret.push_back(root);
}
}
}
return ret;
}
};

tips:

直接学习大神的代码

http://bangbingsyb.blogspot.sg/2014/11/leetcode-unique-binary-search-trees-i-ii.html

一开始一直有一个疑问,如果min==max的时候(即只有一个元素的时候)能构造一个新的节点返回么?

肯定是可以的。因为这时left返回的是含有一个NULL的vector,right返回的是含有一个NULL的vector;两个vector的长度都是1,因此可以构造出这个新的点。

===================================================

第二次过这道题,没啥可说的,再学一遍前人的代码。没啥可说的,递归确实很漂亮。

这里的精髓在于,即使是begin>end这种情况,也返回一个长度为1的NULL点;这样做的好处是即使只有一个点传入了,l.size() 和r.szie()也都是1(虽然里面都是NULL);这样代码就很简洁了。

/**
* 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)
{
return Solution::generate(, n);
}
static vector<TreeNode*> generate(int begin, int end)
{
vector<TreeNode* > ret;
if ( begin>end )
{
ret.push_back(NULL);
return ret;
}
for ( int i=begin; i<=end; ++i )
{
vector<TreeNode*> l = Solution::generate(begin, i-);
vector<TreeNode*> r = Solution::generate(i+, end);
for ( int j=; j<l.size(); ++j )
{
for ( int k=; k<r.size(); ++k )
{
TreeNode* root = new TreeNode(i);
root->left = l[j];
root->right = r[k];
ret.push_back(root);
}
}
}
return ret;
}
};

【Unique Binary Search Trees II】cpp的更多相关文章

  1. 【二叉查找树】02不同的二叉查找树个数II【Unique Binary Search Trees II】

    提到二叉查找树,就得想到二叉查找树的递归定义, 左子树的节点值都小于根节点,右子树的节点值都大于根节点. +++++++++++++++++++++++++++++++++++++++++++++++ ...

  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】Unique Binary Search Trees II

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

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

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

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

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

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

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

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

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

随机推荐

  1. php实现在线下载程序安装包功能

    在线下载程序安装包可以很方便在服务器端下载各种程序安装包(Discuz!.phpwind.Dedecms.WordPress....等一些常用程序)并存储在服务器,大大减少站长上传程序安装包时间.默认 ...

  2. 安装 android sdk 不能更新问题

    1 要更改host 文件 2在Android SDK Manager的Tool->Option中按照如下修改

  3. javascript 数组对象与嵌套循环写法

    'use strict' var info=[{"name":"最近想跳河","interst":["历史"," ...

  4. ios网络:应用一个请求的7个步骤

    Splitting big tasks into small tasks is often one of the best ways to solve a problem. Thus, in the ...

  5. C#关于Sort排序问题

    1.在集合中用Sort对集合元素进行排序 List<,,,,}; tmp.Sort((x, y) => -x.CompareTo(y)); Console.WriteLine(tmp); ...

  6. LogStash 中字段的排除和数据的排除

    排除字段 字段的排除需要在filter中进行操作,使用一个叫做 mutate 的工具,具体操作如下 由于这个工具的名字不是很容易联想到,也是找了好一会. //比如我们可能需要避免日志中kafka的一些 ...

  7. IEnumerable 接口 实现foreach 遍历 实例

    额 为啥写着东西? 有次面试去,因为用到的时候特别少 所以没记住, 这个单词 怎么写! 经典的面试题: 能用foreach遍历访问的对象的要求? 答:  该类实现IEnumetable 接口   声明 ...

  8. pure css做的pc登陆界面

    源码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  9. WPF DragDrop事件元素跟随

    前一段时间项目里面要实现一个鼠标拖动一个元素到另外一个元素上面并且赋值的功能,由于要在surface上运行,拖动的时候手指会挡住系统默认的拖动图标,导致用户意识不到自己是不是在拖动着东西,所以要解决这 ...

  10. lnmp的使用

    命令 1.状态管理 lnmp {start|stop|reload|restart|kill|status} 2.添加虚拟host lnmp vhost add