Java实现 LeetCode 95 不同的二叉搜索树 II(二)
95. 不同的二叉搜索树 II
给定一个整数 n,生成所有由 1 … n 为节点所组成的二叉搜索树。
示例:
输入: 3
输出:
[
[1,null,3,2],
[3,2,null,1],
[3,1,null,null,2],
[2,1,3],
[1,null,2,null,3]
]
解释:
以上的输出对应以下 5 种不同结构的二叉搜索树:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
class Solution {
public List<TreeNode> generateTrees(int n) {
if(n == 0)
return new LinkedList<TreeNode>();
return generateTrees(1,n);
}
public List<TreeNode> generateTrees(int start,int end) {
List<TreeNode> res = new LinkedList<TreeNode>();
if(start > end){
res.add(null);
return res;
}
for(int i = start;i <= end;i++){
List<TreeNode> subLeftTree = generateTrees(start,i-1);
List<TreeNode> subRightTree = generateTrees(i+1,end);
for(TreeNode left : subLeftTree){
for(TreeNode right : subRightTree){
TreeNode node = new TreeNode(i);
node.left = left;
node.right = right;
res.add(node);
}
}
}
return res;
}
}
Java实现 LeetCode 95 不同的二叉搜索树 II(二)的更多相关文章
- 95. 不同的二叉搜索树 II
95. 不同的二叉搜索树 II 题意 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 解题思路 这道题目是基于不同的二叉搜索树进行改进的: 对于连续整数序列[left, ri ...
- LeetCode-095-不同的二叉搜索树 II
不同的二叉搜索树 II 题目描述:给你一个整数 n ,请你生成并返回所有由 n 个节点组成且节点值从 1 到 n 互不相同的不同 二叉搜索树 .可以按 任意顺序 返回答案. 二叉搜索树(Binary ...
- 不同的二叉搜索树&II
不同的二叉搜索树 只要求个数,递推根节点分割左右子树即可 class Solution { public int numTrees(int n) { int []dp=new int[n+1]; fo ...
- [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 ...
- [LeetCode] 95. 不同的二叉搜索树 II
题目链接 : https://leetcode-cn.com/problems/unique-binary-search-trees-ii/ 题目描述: 给定一个整数 n,生成所有由 1 ... n ...
- 95题--不同的二叉搜索树II(java、中等难度)
题目描述:给定一个整数 n,生成所有由 1 ... n 为节点所组成的 二叉搜索树 . 示例如下: 分析:这一题需要对比LeetCode96题来分析:https://www.cnblogs.com/K ...
- LeetCode(95): 不同的二叉搜索树 II
Medium! 题目描述: 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], ...
- LeetCode 95——不同的二叉搜索树 II
1. 题目 2. 解答 以 \(1, 2, \cdots, n\) 构建二叉搜索树,其中,任意数字都可以作为根节点来构建二叉搜索树.当我们将某一个数字作为根节点后,其左边数据将构建为左子树,右边数据将 ...
随机推荐
- java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized的报错问题
url的问题 这个是根据使用的数据库版本不同而出现的错误,mysql8.0以上版本在使用URL时如果使用的语句是 url=jdbc:mysql://localhost:3306/book 是会报错的, ...
- 05JAVA基础方法
一.格式 函数有返回值 public static 返回类型 方法名(参数类型 形参1,参数类型 形参2){ 函数体; return 返回值;//返回值必须是定义的返回类型 } 函数没有有返回值 pu ...
- tp5.1使用路径常量
echo "app_path=========".Env::get('app_path')."</br>"; echo "root_pat ...
- 00005-js 获取uuid
admin.guid = function () { function S4() { return (((1+Math.random())*0x10000)|0).toString(16).subst ...
- html,body设置高度100%后底部有空白的bug
html,body{ //... height:100% } #app{ //... } #footbar{ position:absolute;bottom:0px;left:0px;right:0 ...
- P4525 【模板】自适应辛普森法1
P4525 [模板]自适应辛普森法1 #include <bits/stdc++.h> using namespace std; ; double a, b, c, d, l, r; in ...
- nginx配置之负载均衡
nginx负载均衡 nginx代理机分发到多台同一项目的服务机 负载均衡器代理机配置:nginx.conf的http{}: #代理池,运行在不同服务机的程序 upstream loadtest{ se ...
- CF834D
题目链接:http://codeforces.com/contest/834/problem/D 题目大意:将一个有n个数的数列分成k段,每段的价值为该段中不同数字的个数,求k段的最大总价值. 解题思 ...
- java后端解决跨域
1 package com.zys.boot_jeep.config; import org.springframework.context.annotation.Bean; import org.s ...
- 要小心 JavaScript 的事件代理
我们知道,如果给 form 里面的 button 元素绑定事件,需要考虑它是否会触发 form 的 submit 行为.除此之外,其它场合给 button 元素绑定事件,你几乎不用担心这个事件会有什么 ...