给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?
例如,
给出 n = 3,则有 5 种不同形态的二叉查找树:
   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3
详见:https://leetcode.com/problems/unique-binary-search-trees-ii/description/

Java实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<TreeNode> generateTrees(int n) {
if(n<1){
return new ArrayList<TreeNode>();
}
return generateTrees(1,n);
}
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;
}
}

095 Unique Binary Search Trees II 不同的二叉查找树 II的更多相关文章

  1. Java for LeetCode 095 Unique Binary Search Trees II

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

  2. LeetCode(96) Unique Binary Search Trees

    题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...

  3. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  4. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  5. 41. Unique Binary Search Trees && Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  6. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  7. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  8. Unique Binary Search Trees I & II

    Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...

  9. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

随机推荐

  1. 使用ffmpeg添加logo

    1 网上搜出的一些ffmpeg添加logo的命令都不成功,调查了官方手册后以下这种用法成功: ffmpeg -y -i input.mp4 -vf "movie=logo.png [logo ...

  2. hdu 2188 悼念512汶川大地震遇难同胞——选拔志愿者(Bash Game)

    题意:从0开始捐款,每次不超过m元,首先达到n元的获胜 思路:等同于从n开始,每次取不超过m,首先达到0的获胜.(Bash Game) #include<iostream> #includ ...

  3. java学习之super关键字

    对于具有public或者protected属性的父类,其子类若想继承父亲的属性或者方法,那么需要用到super 实例:

  4. bzoj2806

    广义后缀自动机+二分+单调队列+dp 这道题其实就是一个简单dp,dp[i]表示匹配到i最长匹配多少,设val[i]表示当前位置和原串的最长公共长度,二分的长度是L,那么要求dp[i]=max(dp[ ...

  5. redis 操作使用

    /*1.Connection*/ $redis = new Redis(); $redis->connect('127.0.0.1',6379,1);//短链接,本地host,端口为6379,超 ...

  6. Maven安装,配置及更改本地资源库

    安装 1.确保已安装JDK,和配置JAVA_HOME环境变量 . 注:Maven 3.2 要求 JDK 1.6 或以上版本, 而 Maven 3.0/3.1 需要 JDK 1.5 或以上,这里安装的是 ...

  7. python 之 staticmethod,classmethod,property的区别

    绑定方法和非绑定方法: 普通def定义的都是绑定给对象的方法,对象调用时会自动传入对象本事,而类调用时需手动传入对象. 加上@classmethod装饰器就是绑定给类的方法,会自动传类本身 加上@st ...

  8. IDL(Interactive Data Language——交互式数据语言)

    Interactive Data Language——交互式数据语言 目前,图像处理常用的ENVI就是用IDL开发的经典软件 广泛支持的平台: Microsoft Windows Open VMS S ...

  9. supervisor简明教程

    一.supervisor是什么 Linux的后台进程运行有好几种方法,例如nohup,screen等,但是,如果是一个服务程序,要可靠地在后台运行,我们就需要把它做成daemon,最好还能监控进程状态 ...

  10. Git之通过ssh clone代码

    1.git平台:码云 2.服务器系统:Linux 1.在Linux中创建ssh公钥,将创建的公钥添加到码云的ssh公钥管理 2.一般来说我们配置完站点之后,都会生成一个站点对应的文件夹,进入文件夹,然 ...