LeetCode OJ 95. 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
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
这道题目要求构建出存储1...n的所有平衡二叉树,并且这些树是相互独立的,如果有M棵树,那么每个节点都要被new M次。我的想法就是遍历1~n,构建出每一个数字作为根节点时它的左子树和右子树,然后根节点加所有左右子树的组合就是我们要构建的BST。很明显这是递归的思想,代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<TreeNode> list = new ArrayList();
public List<TreeNode> generateTrees(int n) {
if(n<=0) return list;
list = trees(1, n);
return list;
} public List<TreeNode> trees(int n, int m){ //构建BST
List<TreeNode> clist = new ArrayList();
if(m<n){
clist.add(null);
return clist;
}
if(m==n){
TreeNode node = new TreeNode(n);
clist.add(node);
return clist;
}
for(int i = n; i<=m; i++){
List<TreeNode> llist = trees(n, i-1);//构建所有的左子树
List<TreeNode> rlist = trees(i+1, m);//构建所有的右子树
for(TreeNode lnode:llist){ //生成以i为根节点的所有BST
for(TreeNode rnode:rlist){
TreeNode root = new TreeNode(i);
root.left = copyTree(lnode);
root.right = copyTree(rnode);
clist.add(root);
}
}
}
return clist;
} public TreeNode copyTree(TreeNode root){ //复制二叉树
if(root==null) return null;
else{
TreeNode croot = new TreeNode(root.val);
croot.left = copyTree(root.left);
croot.right = copyTree(root.right);
return croot;
}
}
}
LeetCode OJ 95. Unique Binary Search Trees II的更多相关文章
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【一天一道LeetCode】#95. Unique Binary Search Trees II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [leetcode tree]95. Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)
题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...
- 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(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
随机推荐
- CMA-连续内存分配
CMA: Contignous Memory Allocator,连续内存分配,一般是分配给Camera,HDMI等使用,避免预留大块内存 1.声明连续内存 使用dma_contignous_rese ...
- 《JavaScript高级程序设计》读书笔记 ---基本包装类型
为了便于操作基本类型值,ECMAScript 还提供了3 个特殊的引用类型:Boolean.Number 和String.这些类型与本章介绍的其他引用类型相似,但同时也具有与各自的基本类型相应的特殊行 ...
- Tomcat下log4j设置文件路径和temp目录
转自:http://www.cnblogs.com/dkblog/archive/2007/07/27/1980873.html 在Web应用中的如何设置日志文件的路径呢?最笨的方法是写绝对路径,但很 ...
- 添加JUnit到Java Build Path
1.第一种 新建项目,点击右键,选择properties->Java Build Path->Libraries->add library->JUnit->JUnit4- ...
- javaWEB总结(14):请求的转发和重定向
通俗理解请求转发与重定向的流程 通俗的来讲:我们可以把请求转发和重定向当成是一种借钱的情景模式. (1)请求的转发:A向B借钱,B自己没有钱,但是向C借到了钱,并且把钱借给了A.A只向B请求了一次. ...
- 第一百零六节,JavaScript变量作用域及内存
JavaScript变量作用域及内存 学习要点: 1.变量及作用域 2.内存问题 JavaScript的变量与其他语言的变量有很大区别.JavaScript变量是松散型的(不强制类型)本质,决定了它只 ...
- html5权威指南:标记文字
html5权威指南-第八章-用基本的文字元素标记内容 :http://www.cnblogs.com/yc-755909659/archive/2016/10/02/5928122.html html ...
- <hr/>标签改变颜色注意事项
1.css改变颜色 <hr style="border:0;background-color:#093;height:1px;"> 注意: 如果不加border:0 ...
- javascript apply()和call()
原文链接 http://www.jb51.net/article/30883.htm 想要理解透彻apply()和call() ,还要需要理解this 作用域 局部变量 全局变量 js apply ...
- 数学#扩展欧几里德 POJ 1061&2115&2891
寒假做的题了,先贴那时写的代码. POJ 1061 #include<iostream> #include<cstdio> typedef long long LL; usin ...