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 example,
Given n = 3, your program should return all 5 unique BST's shown below.
解题思路:
参考Java for LeetCode 096 Unique Binary Search Trees思路,本题很容易解决。注意,如果试图不复制而使用对象的引用,是无法得到正确结果的,当然也可采用DFS的思路,但是考虑到JAVA面向对象的特征,我们还是采用Java for LeetCode 096 Unique Binary Search Trees的思路吧。
JAVA实现如下:
static public List<TreeNode> generateTrees(int n) {
List<TreeNode> list = new ArrayList<TreeNode>();
if (n == 0) {
list.add(null);
return list;
}
if (n == 1) {
list.add(new TreeNode(1));
return list;
}
for (int k = 0; k <= n - 1; k++) {
List<TreeNode> left = generateTrees(k);
List<TreeNode> right = generateTrees(n - k - 1);
for (TreeNode rightNode : right)
plus(rightNode, k + 1);
for (TreeNode leftNode : left) {
for (TreeNode rightNode : right) {
TreeNode TN = new TreeNode(k + 1);
TN.left = treeNodeClone(leftNode);
TN.right = treeNodeClone(rightNode);
list.add(TN);
}
}
}
return list;
}
public static void plus(TreeNode root, int num) {
if (root == null)
return;
root.val += num;
plus(root.left, num);
plus(root.right, num);
}
public static TreeNode treeNodeClone(TreeNode TN1){
if(TN1==null)
return null;
TreeNode TN2=new TreeNode(TN1.val);
TN2.left=treeNodeClone(TN1.left);
TN2.right=treeNodeClone(TN1.right);
return TN2;
}
Java for LeetCode 095 Unique Binary Search Trees II的更多相关文章
- [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给定节点形成不同BST的集合
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 ...
- 【leetcode】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 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
- Java for LeetCode 096 Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- leetcode 95 Unique Binary Search Trees II ----- java
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- 【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 ...
随机推荐
- EV根证书
Extended Validation SSL Certificates翻译为扩展验证型服务器证书(EV 服务器证书 或 EV SSL证书),申请该证书需要经过最彻底的身份验证,确保证书持有组织的真实 ...
- Failed to fetch URL http://dl-ssl.google.com/android/repository/repository.xml, reason:
Failed to fetch URL http://dl-ssl.google.com/android/repository/repository.xml, reason: Connection t ...
- 版本控制工具:SVN和Maven的区别
一.只有svn的情况 首先考虑没有maven的情况.这样的话,项目组每个开发人员,都需要在本地check out所有的源码. 每次提交之前,需要先更新周边工程的代码.由于工程之间是依赖的,所以很可能需 ...
- AOP技术应用于安全防御与漏洞修复
本文用到的技术 AOP ESAPI 关于AOP技术 AOP(Aspect-Oriented Programming)面向切面编程.切面是什么?切面表示从业务逻辑分离出来的横切逻辑,比如性能监控.日志记 ...
- linux系统中毒排查学习记录
linux有许多的版本,主要关注redhat(centos)和ubuntu这两个主流版本 以下命令基本都需要root权限,执行命令前记得加sudo 第一步 top,ps命令查看系统资源和负载情况,查看 ...
- ClassPathXmlApplicationContext和FileSystemXmlApplicationContext区别
ClassPathXmlApplicationContext 默认文件路径是src下那一级classpath:和classpath*:的区别: classpath: 只能加载一个配置文件,如果配置了多 ...
- from: Java开发必须要知道的知识体系
from: https://zhuanlan.zhihu.com/p/21895647 作者:靳洪飞链接:https://zhuanlan.zhihu.com/p/21895647来源:知乎著作权归 ...
- Web前端开发规范收集
在Web开发中,后端跟前端配合非常easy出现故障.这时我们就须要一些规则来约束前端任意的编写. CSS编程规范 1. 属性书写基本顺序 a. 先位置属性(position, to ...
- Python setup.py和MANIFEST.in文件
Setup.py文件 from setuptools import setup from codecs import open # 第三方依赖包及版本号 requires = ['beautifuls ...
- element的el-tabs控制,以及el-select 多选默认值
一.el-tabs 1.element自己已经封装好了,当切换时v-model的值自动切换为el-tabs-pane的name对应的值. 如下: <el-tabs v-model='active ...