[LeetCode] 95. 不同的二叉搜索树 II ☆☆☆(递归,n个数组成的所有二叉搜索树)
描述
给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。
示例:

解析
利用一下查找二叉树的性质。左子树的所有值小于根节点,右子树的所有值大于根节点。
所以如果求 1...n 的所有可能。
我们只需要把 1 作为根节点,[ ] 空作为左子树,[ 2 ... n ] 的所有可能作为右子树。
2 作为根节点,[ 1 ] 作为左子树,[ 3...n ] 的所有可能作为右子树。
3 作为根节点,[ 1 2 ] 的所有可能作为左子树,[ 4 ... n ] 的所有可能作为右子树,然后左子树和右子树两两组合。
4 作为根节点,[ 1 2 3 ] 的所有可能作为左子树,[ 5 ... n ] 的所有可能作为右子树,然后左子树和右子树两两组合。
...
n 作为根节点,[ 1... n ] 的所有可能作为左子树,[ ] 作为右子树。
至于,[ 2 ... n ] 的所有可能以及 [ 4 ... n ] 以及其他情况的所有可能,可以利用上边的方法,把每个数字作为根节点,然后把所有可能的左子树和右子树组合起来即可。
如果只有一个数字,那么所有可能就是一种情况,把该数字作为一棵树。而如果是 [ ],那就返回 null。
代码
public List<TreeNode> generateTrees(int n) {
List<TreeNode> ans = new ArrayList<TreeNode>();
if (n == 0) {
return ans;
}
return getAns(1, n);
}
private List<TreeNode> getAns(int start, int end) {
List<TreeNode> ans = new ArrayList<TreeNode>();
//此时没有数字,将 null 加入结果中
if (start > end) {
ans.add(null);
return ans;
}
//只有一个数字,当前数字作为一棵树加入结果中
if (start == end) {
TreeNode tree = new TreeNode(start);
ans.add(tree);
return ans;
}
//尝试每个数字作为根节点
for (int i = start; i <= end; i++) {
//得到所有可能的左子树
List<TreeNode> leftTrees = getAns(start, i - 1);
//得到所有可能的右子树
List<TreeNode> rightTrees = getAns(i + 1, end);
//左子树右子树两两组合
for (TreeNode leftTree : leftTrees) {
for (TreeNode rightTree : rightTrees) {
TreeNode root = new TreeNode(i);
root.left = leftTree;
root.right = rightTree;
//加入到最终结果中
ans.add(root);
}
}
}
return ans;
}
[LeetCode] 95. 不同的二叉搜索树 II ☆☆☆(递归,n个数组成的所有二叉搜索树)的更多相关文章
- LeetCode 107. Binary Tree Level Order Traversal II (二叉树阶层顺序遍历之二)
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- Java实现 LeetCode 95 不同的二叉搜索树 II(二)
95. 不同的二叉搜索树 II 给定一个整数 n,生成所有由 1 - n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], [3,1, ...
- LeetCode 95 | 构造出所有二叉搜索树
今天是LeetCode专题第61篇文章,我们一起来看的是LeetCode95题,Unique Binary Search Trees II(不同的二叉搜索树II). 这道题的官方难度是Medium,点 ...
- 95. 不同的二叉搜索树 II
95. 不同的二叉搜索树 II 题意 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 解题思路 这道题目是基于不同的二叉搜索树进行改进的: 对于连续整数序列[left, ri ...
- [leetcode]95.不同的二叉搜索树
Posted by 微博@Yangsc_o 原创文章,版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0 95. 不同的二叉搜索树 II 给你 ...
- LeetCode-095-不同的二叉搜索树 II
不同的二叉搜索树 II 题目描述:给你一个整数 n ,请你生成并返回所有由 n 个节点组成且节点值从 1 到 n 互不相同的不同 二叉搜索树 .可以按 任意顺序 返回答案. 二叉搜索树(Binary ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- 不同的二叉搜索树&II
不同的二叉搜索树 只要求个数,递推根节点分割左右子树即可 class Solution { public int numTrees(int n) { int []dp=new int[n+1]; fo ...
随机推荐
- 用KNN实现iris的4分类问题&测试精度
import matplotlib.pyplot as plt from scipy import sparse import numpy as np import matplotlib as mt ...
- System.getProperties 获取当前的系统属性
getProperties public static Properties getProperties() 确定当前的系统属性. 首先,如果有安全管理器,则不带参数直接调用其 checkProper ...
- xps转换为pdf
https://www.cnblogs.com/zeoy/archive/2013/01/09/2852941.html https://blog.csdn.net/jafucong/article/ ...
- 【warning】set the environment variable MXNET_CUDNN_AUTOTUNE_DEFAULT to 0 to disable
前言 运行mxnet程序的时候出现这个warning信息,但是不影响整个程序的运行,不过宝宝喜欢将warning信息尽量也clear,强迫症嘛?!哈哈哈哈 问题描述 [::] src/operator ...
- No section matches selector - no section to be FIRST/LAST
1. 使用KEIL MDK ,STM32F405RG,编译的时候报错 .\Objects\ks3620_stm32f405_proj.sct(): error: L6236E: No section ...
- Cas(09)——通过Proxy访问其它Cas应用
通过Proxy访问其它Cas应用 目录 1.1 原理 1.2 配置 1.2.1 代理端 1.2.2 被代理端 1.3 请求示例 考虑这样一种场景:有两个应用App1 ...
- mac的公式编辑器: mathtype/latex
mathtype 收费版,且马上不能在mac系统上使用 latex 搜索一下: ➜ ~ brew search latex ==> Formulae gnome-latex latex2html ...
- 查询SQL时间段内执行过的哪些SQL语句
QS.creation_time, ) , (( ) ) AS statement_text, ST.text, QS.total_worker_time, QS.last_worker_time, ...
- C++编译提示 default argument are given of parameter ..
如果定义一个类的构造函数时,带有默认的入参值,在cpp文件中实现构造函数时,是不能带的!否则就会提示该种类型的编译错误. //.h文件: namespace Ui { class Task; } cl ...
- QT 源码分析--1
Ref: http://blog.sina.com.cn/s/blog_6e80f1390100qoc0.html 安装qt之后(我使用的是online自动安装),安装目录下有\5.10.1\Src\ ...