Unique Binary Search Trees II leetcode java

【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

Unique Binary Search Trees II -- LeetCode

描述

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.

   1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

解析

这道题比1难的就是不是返回个数,而是返回所有结果。
引用code ganker(http://codeganker.blogspot.com/2014/04/unique-binary-search-trees-ii-leetcode.html)的讲解:
”这道题是求解所有可行的二叉查找树,从Unique Binary Search Trees中我们已经知道,可行的二叉查找树的数量是相应的卡特兰数,不是一个多项式时间的数量级,所以我们要求解所有的树,自然是不能多项式时间内完成的了。算法上还是用求解NP问题的方法来求解,也就是N-Queens中
介绍的在循环中调用递归函数求解子问题。思路是每次一次选取一个结点为根,然后递归求解左右子树的所有结果,最后根据左右子树的返回的所有子树,依次选取
然后接上(每个左边的子树跟所有右边的子树匹配,而每个右边的子树也要跟所有的左边子树匹配,总共有左右子树数量的乘积种情况),构造好之后作为当前树的
结果返回。

这道题的解题依据依然是:
当数组为 1,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:
以i为根节点的树,其左子树由[1, i-1]构成, 其右子树由[i+1, n]构成。

在做 Unique Binary Search Trees 这道题时,我们用一个数组保存 1 至 n-1 对应的不同二叉树的个数 X1、X2、X3、... Xn-1

则 n 对应的不同二叉树个数Xn = Xn-1 + X1*Xn-2 + X2*Xn-3 + X3*Xn-4 + ... + Xn-2*X1 + Xn-1

  通过这个递推式,我们可以从 N = 1 开始递推,最后得到 N = n 时不同二叉查找树的个数。

与上述思路类似,我们可以通过深度优先搜索(递归)解决这道题。

因为二叉查找树满足父节点的值大于左子节点的值,小于右子节点的值,所以我们可以:

(1) 从 N=1 开始构建二叉查找树,则它的左子树节点数为 0,右子树节点数为 n-1;

(2) N=2 时,左子树节点数为 1,右子树节点数为 n-2;

……

(n) N=n 时,左子树节点数为 n-1,右子树节点数 0。

而在第(1)步中,右子树继续执行上述循环,子树的子树又执行这个循环,最终,我们可以将子树节点数减少到 1,而一个节点只有一种排列方式,所以此时可以毫不犹豫地将结果返回给上一级。然后包含有两个节点的二叉树排列方式又被返回给上一级。……

依此类推,我们最后可以得到所有不同结构的二叉查找树。

代码

public ArrayList<TreeNode> generateTrees(int n) {
return generateTrees(1, n);//从1作为root开始,到n作为root结束
} private ArrayList<TreeNode> generateTrees(int left, int right){
ArrayList<TreeNode> res = new ArrayList<TreeNode>();
if (left > right){
res.add(null);//加入一个空元素进去来表示这是一颗空树 并且同时也是保证下面循环时即使一边是空树,也会跑另一边
return res;
}
for (int i = left; i <= right; i++){
ArrayList<TreeNode> lefts = generateTrees(left, i-1);//以i作为根节点,左子树由[1,i-1]构成
ArrayList<TreeNode> rights = generateTrees(i+1, right);//右子树由[i+1, n]构成
for (int j = 0; j < lefts.size(); j++){
for (int k = 0; k < rights.size(); k++){
TreeNode root = new TreeNode(i);
root.left = lefts.get(j);
root.right = rights.get(k);
res.add(root);//存储所有可能行
}
}
}
return res;
}

[LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆的更多相关文章

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

  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. 集成算法——Ensemble learning

    目的:让机器学习效果更好,单个不行,群殴啊! Bagging:训练多个分类器取平均 Boosting:从弱学习器开始加强,通过加权来进行训练 (加入一棵树,比原来要强) Stacking:聚合多个分类 ...

  2. 2. mysql 语句

    基础语句 创建表 DROP TABLE IF EXISTS student;CREATE TABLE student ( id ) NOT NULL AUTO_INCREMENT, sname ) N ...

  3. Tomcat 跨域问题的解决

    先下载CORS对应的Jar: 下载链接: https://download.csdn.net/download/u010739157/10565169 在tomcat的Web.xml中加上如下配置: ...

  4. border:none和border:0的区别

    C:当定义border:none时,表示无边框样式,浏览器并不会对边框进行渲染,也就没有实际的宽度:  D:定义边框时,除了设置宽度外,还必须设置边框的样式才能显示出来.     border:0;浏 ...

  5. 33 Python 详解命令解析 - argparse--更加详细--转载

    https://blog.csdn.net/lis_12/article/details/54618868 Python 详解命令行解析 - argparse Python 详解命令行解析 - arg ...

  6. 异常处理.VC++

    ZC:个人这样 理解 C++的异常处理: ZC: (1).C++标准异常处理,try{}catch{} 抛异常:throw() [ 据说是包装的Windows函数RaiseException() ] ...

  7. C++.【转】C++数值类型与string的相互转换

    1.C++数值类型与string的相互转换 - JohnGu - 博客园.html(https://www.cnblogs.com/johngu/p/7878029.html) 2. 1.数值类型转换 ...

  8. Android自定义权限

    一.自定义权限 自定义权限,一般是考虑到应用共享组件时的安全问题.我们知道在四大组件 AndroidManifest 中注册的时候,添加 exported = "true" 这一属 ...

  9. 常见字符集&乱码问题

    字符集 常用字符集分类 ASCII及其扩展字符集 作用:表语英语及西欧语言. 位数:ASCII是用7位表示的,能表示128个字符:其扩展使用8位表示,表示256个字符. 范围:ASCII从00到7F, ...

  10. Import Projects from git

    1:工具栏window -->show view -->Other