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 ...
随机推荐
- Digi. Certificates: Key pairs usages
In short, we have some sort of algorithms to gen pair of private and public keys. The public key is ...
- 4.当接口的请求方式为 application/json的时候时
1..当接口的请求方式为 application/json的时候时,使用抓包软件(fiddler)获取到这个接口, 其中的Inspectprs-TextView中的内容就是jmeter中Body Da ...
- $.ajax({})方法success,error,complete,beforeSend使用例子及解释
在与后台交互的时候,经常使用到jquery的$.ajax()方法来请求数据: 回调函数用的比较多的是success,但是complete.beforeSend.error函数也是很有用的: 下面是使用 ...
- visual SVN 反编译破解
今天发现visual SVN 过期了.网上搜索了一下,发现了下面的贴子. http://www.heiqu.com/show-71200-1.html 一看是用.Net写的,大喜,破解就太简单了.本来 ...
- win10下安装Django
Django的核心(1.4+)可以运行在从2.5到2.7之间的任何Python版本. 我的电脑是操作系统是window10 ,内存是4G. 1.下载django 官网地址:https://www.dj ...
- Linux的cron和crontab
一 cron crond位于/etc/rc.d/init.d/crond 或 /etc/init.d 或 /etc/rc.d /rc5.d/S90crond,最总引用/var/lock/subsys/ ...
- Java、C#双语版配套AES加解密示例
这年头找个正经能用的东西那是真难,网上一搜索一大堆,正经能用的没几个,得,最后还是得靠自己,正巧遇上需要AES加解密的地方了,而且还是Java和C#间的相互加解密操作,这里做个备忘 这里采用的加解 ...
- 一行一行分析JQ源码学习笔记-02
1.防止冲突 设置新变量保存
- Dubbo协议与连接控制
协议参考手册 (+) (#) 推荐使用Dubbo协议 性能测试报告各协议的性能情况,请参见:性能测试报告 (+) dubbo:// (+) (#) Dubbo缺省协议采用单一长连接和NIO异步通讯,适 ...
- JAVASCRIPT 框架>>
jQuery jQueryjQuery 是目前最受欢迎的 JavaScript 框架 jQuery 是为处理 HTML 事件而特别设计的<script type="text/javas ...