【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 ...
随机推荐
- Effective Objective-C 2.0 — 第11条:理解 objc_msgSend 的作用
消息由接受者.选择子及参数构成.给某对象“发送消息” (invoke a message) 也就相当于在该对象上“调用方法”(call a method) 发给某对象的全部信息都要由“动态消息派发系统 ...
- java 递归获取一个目录下的所有文件路径
还是日志的问题,log4j生成的日志文件,自动保存到月份所在的文件夹中,需要获取到所有的日志文件,包括文件夹 private List<String> ergodic(File file, ...
- [Exchange]使用EWS托管API2.0同步邮箱
你可以通过Exchange Web Serivice(EWS)托管API去检索从一个给定的时间点,文件夹中有变化的列表中的项. 客户端可以使用SyncFoldersItems方法,同步服务端的项目,你 ...
- hdu4941 Magical Forest (stl map)
2014多校7最水的题 Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit ...
- vijos1741 观光公交 (贪心)
https://www.vijos.org/p/1741 P1741观光公交 请登录后递交 标签:NOIP提高组2011[显示标签] 描述 风景迷人的小城Y市,拥有n个美丽的景点.由于慕名而来的游 ...
- git SSH keys
An SSH key allows you to establish a secure connection between your computer and GitLab. Before gene ...
- POJ 3071 Football
很久以前就见过的...最基本的概率DP...除法配合位运算可以很容易的判断下一场要和谁比. from——Dinic算法 Football Time ...
- HDOJ 4739 Zhuge Liang's Mines
Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Mac Pro 解压安装MySQL二进制分发版 mysql-5.6.30-osx10.11-x86_64.tar.gz(不是dmg的)
没有mac的root密码,当前用户有sudo权限,所以想以root身份执行的命令都加了sudo. 是否存在 _mysql 用户和用户组,并查看用户 _mysql 是不是用户组 _mysql 的成员. ...
- SQL Server 查询、搜索命令、语句
--查询所有表 SELECT NAME,* FROM SYSOBJECTS WHERE XTYPE='U' order by SYSOBJECTS.name --查询所有存储过程 select * f ...