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\) 构建二叉搜索树,其中,任意数字都可以作为根节点来构建二叉搜索树.当我们将某一个数字作为根节点后,其左边数据将构建为左子树,右边数据将 ...
随机推荐
- python之邮件发送自动化
# -*- coding:utf-8 -*-#@Time : 2020/3/24 22:55#@Autor: Mr.White#@File : 发送邮件.py 一.导入所需要的类 import smt ...
- 你的团队需要一个正确的程序集(dll)管理姿势
很多团队经历时间的积淀之后,都会有很多的可重用的公共技术组件.大部分的团队都会把这些公共组件生成程序集(dll)后,放到GIT或SVN的一个公共目录里面,以供各个项目中使用.起初在项目很少又或者是公共 ...
- Js 事件基础
一:js中常见得事件 (1) : 鼠标事件 click :点击事件 dblclick :双击事件 contextmenu : 右键单击事件 ...
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- git仓促拉去提交输入密码读取钥匙串
项目的目录执行: git config --global credential.helper osxkeychain
- java ->Servlet接口
JavaWeb核心之Servlet Servlet简介 什么是Servlet(控制器的作用) Servlet 运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求.响应给 ...
- Vi 和 Vim 的使用
Vi (Visual Interface)是 Linux下基于Shell 的文本编辑器,Vim (Visual Interface iMproved)是 Vi的增强版本,扩展了很多功能,比如对程序源文 ...
- DOM的使用
1. 修改: 3样: 1. 内容: 3个属性: 1. 获取或修改原始HTML片段: 元素.innerHTML 2. 获取或修改纯文本内容: 元素.textContent vs innerHTML 1. ...
- 王艳 201771010127《面向对象程序设计(java)》第二周学习总结
王艳 201771010127<面向对象程序设计(java)>第二周学习总结 第一部分:理论知识学习部分 3.1:基本概念. 1)标识符:标识符由字母.数字.美元符号以及下划线组成.且第 ...
- CPU上下文切换以及相关指标的理解
前言 上下文切换这个词一直不理解,看了无数遍就忘了无数遍,知道看到<操作系统导论>这本书,终于有了略微的理解.这也证明了我的方向是没错的,一直认为做运维还是得理解底层的知识,不理解很多 ...