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
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; left = null; right = null; }
* }
*/
public class Solution {
public ArrayList<TreeNode> generateTrees(int n) {
// Start typing your Java solution below
// DO NOT write main() function
return generateTrees(1,n);
}
public ArrayList<TreeNode> generateTrees(int a, int b){
ArrayList<TreeNode> res = new ArrayList<TreeNode>();
if(a>b) res.add(null);
for(int i=a;i<=b;i++){
ArrayList<TreeNode> temp1 = generateTrees(a,i-1);
ArrayList<TreeNode> temp2 = generateTrees(i+1,b);
for(TreeNode n:temp1)
for(TreeNode m:temp2){
TreeNode temp= new TreeNode(i);
temp.left=n;
temp.right=m;
res.add(temp);
}
}
return res;
}
}
Unique Binary Search Trees II的更多相关文章
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 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 ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- 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 ...
- 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]* ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
随机推荐
- ASP连接ACCESS数据库
Set conn2 = Server.CreateObject("ADODB.Connection") conn2.Open "Provider=Microsoft.Je ...
- ADO.NET笔记(一)XML导入导出和数据库
数据导出成XML文件 #region 数据导出成XML文件 string sql = "select Id, CallerNumber, TelNum, StartDateTime, End ...
- UMeng 友盟的用户数,启动数 等
最近一哥们研究UMeng 友盟的用户数,启动数,设备和位置相关数据,发现真的可以模拟啊. 支持Android,IOS,WPhone平台,同时可以实现每小时按10万级的速度增加,启动次数也是可以按10万 ...
- 相似度到大数据查找之Mysql 文章匹配的一些思路与提高查询速度
文章相关度匹配的一些思路---"压缩"预料库,即提取用特征词或词频,量化后以“列向量”形式保存到数据库:按前N组词拼为向量组供查询使用,即组合为1到N字的组合,量化后以“行向量”形 ...
- JSON的使用
最近在项目中大量的使用了JSON, 发现JSON和XML的功能相近,都是一种数据传输格式.只是与XML相比JSON显得更加轻量级,使用也更加容易. JSON依赖的第三方jar包: commons-be ...
- 管理员取得所有权(复制以下代码粘贴至记事本然后把后缀名改为reg)
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\runas] @="获取权限" "NoWo ...
- 关于linux下零散的东西 --慢慢补充
一.截图 ,使用Shift+Ctrl+PrtSc就可以截图. 二.tar命令参数 c:表示压缩 x:表示解压 z:表示gzip的方式解/压缩 j:表示bzip2的方式解/压缩 三.串口终端ke ...
- 简单使用packetbeat
简单使用packetbeat 标签:packetbeat elasticsearch 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在前面两篇文章中记录了使用logstash来收集mysql的慢 ...
- Div 内部所有元素 全部垂直对齐
http://stackoverflow.com/questions/7273338/how-to-vertically-align-an-image-inside-div How it works: ...
- Node.js 【使用npm安装一些包失败之笔记】
镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 1.通过config命令 npm config set registry https://regist ...