Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.

Input: 3
Output:
[
[1,null,3,2],
[3,2,null,1],
[3,1,null,null,2],
[2,1,3],
[1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below: 1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

题意:

给定n个节点,列举可形成的不同的BST集合

思路:

跟 [leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数 相似。

对于给定的n

需要去查[1, n]范围内可生成的BST,如下图,

1       1           2          3       3   ...          i              ... n
\ \ / \ / / / \
3 2 1 3 2 1 [1,i-1] [i+1,n]
/ \ / \
2 3 1 2

我们需要列出

以1为root可生成的所有BST

以2为root可生成的所有BST

......

那么对于任意以 i 为root可生成的所有BST,根据BST的性质:

其左子树的值一定小于i,也就是[1, i - 1]区间,用helper生成list of leftList

而右子树的值一定大于i,也就是[i + 1, n]区间, 用helper生成list of rightList

最后,用root,  leftList中的值,rightList中的值,三者生成BST

代码:

 class Solution {
public List<TreeNode> generateTrees(int n) {
if(n == 0) return new ArrayList<>();
return helper(1, n); // root node from 1 to n
} private List<TreeNode> helper(int left, int right){
List<TreeNode> result = new ArrayList<>();
if(left > right){
result.add (null);
return result;
}
for(int i = left; i <= right; i++){
List<TreeNode> lefts = helper(left, i-1);
List<TreeNode> rights = helper(i+1, right);
for(TreeNode l : lefts){
for(TreeNode r : rights){
TreeNode root = new TreeNode(i);
root.left = l;
root.right = r;
result.add(root);
}
}
}
return result;
}
}

[leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合的更多相关文章

  1. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  2. [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 ...

  3. [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  4. 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 ...

  5. leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  6. [leetcode]95 Unique Binary Search Trees II (Medium)

    原题 字母题添加链接描述 一开始完全没有思路.. 百度看了别人的思路,对于这种递归构造的题目还是不熟,得多做做了. 这个题目难在构造出来.一般构造树都需要递归. 从1–n中任意选择一个数当做根节点,所 ...

  7. LeetCode 95. Unique Binary Search Trees II 动态演示

    比如输入为n, 这道题目就是让返回由1,2,... n的组成的所有二叉排序树,每个树都必须包含这n个数 这是二叉树的排列组合的题目.排列组合经常用DFS来解决. 这道题比如输入为3,也就是求start ...

  8. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  9. 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]* ...

随机推荐

  1. Linux进程调度与抢占

    一.linux内核抢占介绍 1.抢占发生的必要条件 a.preempt_count抢占计数必须为0,不为0说明其它地方调用了禁止抢占的函数,比如spin_lock系列函数.b.中断必须是使能的状态,因 ...

  2. yum源搭建

    1:vim /etc/yum.repo.d/ll.repo [local]   这里不能有空格,如[local yum] name=local baseurl=file:///yum gpgcheck ...

  3. Logistic Loss的简单讨论

    首先应该知道Logistic Loss和Crossing Entropy Loss本质上是一回事. 所以所谓的SoftMaxLoss就是一般二分类LogisitcLoss的推广.之所以在网络中采取这种 ...

  4. 文件-- 字节相互转换(word、图片、pdf...)

    方式一: /// <summary> /// word文件转换二进制数据(用于保存数据库) /// </summary> /// <param name="wo ...

  5. vagrant up报错 Warning: Authentication failure. Retrying...解决方案

    参照链接 https://www.cnblogs.com/zqifa/p/vagrant-1.html 可以解决问题.

  6. ubuntu下的Nessus插件更新

    00x1: 记录下nessus插件离线更新,免得每次度娘我Nessus是放在虚拟机里面. 00x2: nessus 插件更新地址: https://plugins.nessus.org/v2/offl ...

  7. Web App Checklist

    Mobile Web App checklist 目标: 高性能Mobile Web App 一.UX和UI 操作节目与边框之间留空隙: 防止操作过程中,触发系统缺省行为,有些是无法disable的. ...

  8. linux中 bashrc文件的alias添加快捷命令

    alias (为了简化命令操作,节省时间) 进入 /home下的用户,假设为 web 执行命令 ls -alh   找到 .bashrc 隐藏文件,如果没有则新建 通过  vi .bashrc  在里 ...

  9. c# 对DataTable进行分组group by

    ]);//对索引为0的一列进行分组,结果是集合

  10. C#中EXCEL表格的内容进度条实现

    public void ExportToExcel() { DataTable dt = getDataTable(); if (dt == null) { MessageBox.Show(" ...