【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II
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 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) { vector<TreeNode *>result=build(,n);
return result;
} vector<TreeNode *> build(int l,int r)
{ if(l>r)
{
vector<TreeNode *> root();
root[]=NULL;
return root;
} vector<TreeNode *> result; for(int i=l;i<=r;i++)
{
vector<TreeNode *> left=build(l,i-);
vector<TreeNode *> right=build(i+,r);
for(int j=;j<left.size();j++)
{
for(int k=;k<right.size();k++)
{
TreeNode *root=new TreeNode(i);
root->left=left[j];
root->right=right[k];
result.push_back(root); }
}
} return result;
}
};
【leetcode】Unique Binary Search Trees II的更多相关文章
- 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
- 【leetcode】 Unique Binary Search Trees II (middle)☆
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【Leetcode】【Medium】Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【题解】【BST】【Leetcode】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【Leetcod】Unique Binary Search Trees II
给定结点数n,结点值为1,2,...,n,求由这些结点可以构成的所有二叉查找树. Given n, generate all structurally unique BST's (binary sea ...
- 【树】Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- JDBC连接池
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/45/0C/wKiom1PjixLxfknCAAEbp-e9Bq ...
- android自定义控件(8)-利用onMeasure测量使图片拉伸永不变形,解决屏幕适配问题
使用ImageView会遇到的问题 在Android应用中,都少不了图片的显示,ImageView,轮播图,ViewPager等等,很多都是来显示图片的,很多时候,我们都希望图片能够在宽度上填充父窗体 ...
- iOS- Terminating app due to uncaught exception 'NSRangeException'
错误描述: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM object ...
- ubuntu系统无法访问无法磁盘最佳解决办法
出现如下错误: Error mounting /dev/sda8 at /media/fzh/System: Command-line `mount -t "ntfs" -o &q ...
- firefox如何卸载插件plugins和临时文件夹
下载原版的 英文版的 firefox 会看到 openH264 video codec Plugin和microsoft DRM (digit rightcopy manager 数字版权管理)等等插 ...
- Linux服务器管理: 系统管理:系统资源查看
vmstat 命令: 查看或监控系统资源 [root@localhostA1 ~]# vmstat procs -----------memory---------- ---swap-- -----i ...
- systemd
本文参照:https://wiki.archlinux.org/index.php/Systemd#Basic_systemctl_usage 做了翻译和整理 systemd是Linux下的一种ini ...
- MFC中文件的查找、创建、打开、读写等
http://blog.csdn.net/whatforever/article/details/6316416
- [译]Bundling and Minification
原文:http://www.asp.net/mvc/overview/performance/bundling-and-minification============================ ...
- Intent和Activity知识点总结
1.Intent的跳转传值2.Intent的隐式启动(用于不同应用中)与显示启动(同一应用中)3.Activity的生命周期 void onCreate()——Activity已经被创建完毕,创 ...