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

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

这道题目要求构建出存储1...n的所有平衡二叉树,并且这些树是相互独立的,如果有M棵树,那么每个节点都要被new M次。我的想法就是遍历1~n,构建出每一个数字作为根节点时它的左子树和右子树,然后根节点加所有左右子树的组合就是我们要构建的BST。很明显这是递归的思想,代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<TreeNode> list = new ArrayList();
public List<TreeNode> generateTrees(int n) {
if(n<=0) return list;
list = trees(1, n);
return list;
} public List<TreeNode> trees(int n, int m){ //构建BST
List<TreeNode> clist = new ArrayList();
if(m<n){
clist.add(null);
return clist;
}
if(m==n){
TreeNode node = new TreeNode(n);
clist.add(node);
return clist;
}
for(int i = n; i<=m; i++){
List<TreeNode> llist = trees(n, i-1);//构建所有的左子树
List<TreeNode> rlist = trees(i+1, m);//构建所有的右子树
for(TreeNode lnode:llist){ //生成以i为根节点的所有BST
for(TreeNode rnode:rlist){
TreeNode root = new TreeNode(i);
root.left = copyTree(lnode);
root.right = copyTree(rnode);
clist.add(root);
}
}
}
return clist;
} public TreeNode copyTree(TreeNode root){ //复制二叉树
if(root==null) return null;
else{
TreeNode croot = new TreeNode(root.val);
croot.left = copyTree(root.left);
croot.right = copyTree(root.right);
return croot;
}
}
}

LeetCode OJ 95. Unique Binary Search Trees II的更多相关文章

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

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

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

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

  3. 【一天一道LeetCode】#95. Unique Binary Search Trees II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. [leetcode tree]95. Unique Binary Search Trees II

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

  5. LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

    题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...

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

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

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

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

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

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

随机推荐

  1. java 枚举类型和数据二进制等问题思考

    .以下代码的输出结果是什么? int X=100; int Y=200; System.out.println("X+Y="+X+Y); System.out.println(X+ ...

  2. chrome pyv8下载

    url: https://code.google.com/archive/p/pyv8/downloads linux命令: $sudo pip install -v pyv8

  3. SpringMVC通过注解获得参数

    SpringMVC可以通过RequestParam注解来映射获得参数,具体用法如下: 例子: 配置过程省略 1.新建controller类 package com.loger.controller; ...

  4. Ubuntu下安装composer及配置

    1.下载最新composer wget -c https://getcomposer.org/composer.phar 2.可执行权限 chmod u+x composer.phar 3.放置到安装 ...

  5. [妙味JS基础]第九课:定时器管理、函数封装

    知识点总结 函数封装 回调函数 实例:抖动函数 获取当前的位置 通过数组来实现,一正一负,直到恢复成0为止. 当前位置与数组中各值相加

  6. MVC5笔记【一】

    一.global.asax文件的作用:全局性配置文件 理解什么是路由? 有什么作用: 路由主要提供一个路由表 请求的时候被加载,请求url要去路由表当中去对照 规则 解析规则 控制器/动作放方法,转移 ...

  7. 在Wamp 添加站点和域名

    在httpd.conf中使Include conf/extra/httpd-vhosts.conf生效 在conf/extra/httpd-vhosts.conf中加入如下代码 <Virtual ...

  8. FBO

    #include <GL/glew.h> #include <GL/freeglut.h> #include <iostream> #pragma comment( ...

  9. hive UDF添加方式

    hive UDF添加的方式 1.添加临时函数,只能在此会话中生效,退出hive自动失效 hive> add jar /home/jtdata/hiveUDF/out0.jar; Added [/ ...

  10. 禁用 VS2010 的 vcpkgsrv.exe 运行

    VS2010经常使用一段时间后巨卡,发现vcpkgsrv.exe这个进程相当占内存,但是结束后又会自己启动,百度之原来是IntelliSense的问题,关闭之即可,设置如下