095 Unique Binary Search Trees II 不同的二叉查找树 II
给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?
例如,
给出 n = 3,则有 5 种不同形态的二叉查找树:
   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3
详见:https://leetcode.com/problems/unique-binary-search-trees-ii/description/
Java实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<TreeNode> generateTrees(int n) {
if(n<1){
return new ArrayList<TreeNode>();
}
return generateTrees(1,n);
}
private ArrayList<TreeNode> generateTrees(int left, int right){
ArrayList<TreeNode> res = new ArrayList<TreeNode>();
if (left > right){
res.add(null);
return res;
}
for (int i = left; i <= right; i++){
ArrayList<TreeNode> lefts = generateTrees(left, i-1);//以i作为根节点,左子树由[1,i-1]构成
ArrayList<TreeNode> rights = generateTrees(i+1, right);//右子树由[i+1, n]构成
for (int j = 0; j < lefts.size(); j++){
for (int k = 0; k < rights.size(); k++){
TreeNode root = new TreeNode(i);
root.left = lefts.get(j);
root.right = rights.get(k);
res.add(root);//存储所有可能行
}
}
}
return res;
}
}
095 Unique Binary Search Trees II 不同的二叉查找树 II的更多相关文章
- Java for LeetCode 095 Unique Binary Search Trees II
		Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ... 
- LeetCode(96) Unique Binary Search Trees
		题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ... 
- 【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) ... 
- LeetCode:Unique Binary Search Trees I II
		LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ... 
- Unique Binary Search Trees I & II
		Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ... 
- Unique Binary Search Trees,Unique Binary Search Trees II
		Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ... 
随机推荐
- SCAU RP Test —— 因式分解与组合
			D RP Test Time Limit:1000MS Memory Limit:65535K 题型: 编程题 语言: 无限制 描述 LRC是SCAU_ACM校队的主席,职业生涯为校队作过很多 ... 
- linux从用户组中删除某用户
			1. 从wheel组中删除 test用户 gpasswd wheel -d test 2. 给 目录赋予 其他组上传文件的权限 chmod a+w test 
- poj图论解题报告索引
			最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ... 
- WPF-悬浮在底部的导航
			先用Rectangle代替导航按钮,这个导航会悬浮在界面的底部,当鼠标移进导航按钮上的时候,按钮会放大,移出后恢复正常. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 ... 
- Chapter3——进入Android Dalvik虚拟机(二)
			Dalvik汇编语言基础 Dalvik虚拟机为自己设计了一套指令集,并制定了自己的指令格式和调用规范. 位描述约定如下: 每16位的字采用空格分隔开来 每个字母表示4位,每个字母按顺序从高字节开始,排 ... 
- 数组(Array)的初始化
			如果这样: private static int unsorted[]; for(int i = 1 ; i < 8 ; i ++ ) unsorted[i] = 1 ; 是会报NullPoin ... 
- LOJ114 k大异或和
			传送门 (vjudge和hdu也有但是我觉得LOJ好看!而且限制少!) 不过本题描述有误,应该是k小. 首先我们需要对线性基进行改造.需要把每一位改造成为,包含最高位的能异或出来的最小的数. 为啥呢? ... 
- 关于yolov3 训练输出值
			Region xx: cfg文件中yolo-layer的索引: Avg IOU:当前迭代中,预测的box与标注的box的平均交并比,越大越好,期望数值为1: Class: 标注物体的分类准确率,越大越 ... 
- c++之cin/cin.get/cin.getline()详解
			C++输入过程中,是把输入加载到缓冲区中,然后对缓冲区中的字符进行读取.cin,cin,get(),cin.getline()三个函数虽然都能进行数据读取,但是它们对缓冲区内数据的处理方法是不同的(如 ... 
- C++日志之获取函数的名字,行号,文件名
			在后台程序运行出问题时,详尽的日志是抓错不可缺少的帮手,这里提供一个能自动记录日志触发点文件名.行号.函数名的方法,关键是利用C99新增的预处理标识符__VA_ARGS__ 先介绍几个编译器内置的宏定 ... 
