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. tftp服务器最简单安装配置

    注:转载他人 这是在debian下面操作的1.安装tftp-server sudo apt-get install tftpd-hpa sudo apt-get install tftp-hpa(如果 ...

  2. notepad++正则表达式替换字符串详解

    正则表达式是一个查询的字符串,它包含一般的字符和一些特殊的字符,特殊字符可以扩展查找字符串的能力,正则表达式在查找和替换字符串的作用不可忽视,它 能很好提高工作效率. EditPlus的查找,替换,文 ...

  3. util:properties与context:property-placeholder

    spring 使用注解装配的Bean如何使用property-placeholder属性配置中的值 这个问题不大不小,以前偷懒凡是碰到需要引用属性文件中的类时就改用xml来配置. 今天看了下sprin ...

  4. Golang--计算文件的MD5值

    参考: http://blog.csdn.net/u014029783/article/details/53762363 用法: $ go run 01.go -f 1.txt b9d228f114d ...

  5. 单词缩写(abbr.cpp)每日一题

    题目描述:YXY 发现好多计算机中的单词都是缩写,如 GDB,它是全称 Gnu DeBug 的缩写.但是,有时缩写对应的全称并不固定,如缩写 LINUX,可以理解为:(1)LINus's UniX ( ...

  6. day01(RESTful Web Service、SVN)

    今日大纲 搭建SSM环境 基于SSM环境实现用户管理系统 学习RESTful Web Service 学习SVN 统一开发环境 JDK1.7 32? 64? -- 64 Eclipse 使用4.4.1 ...

  7. IOS开发小技巧,知识点

    1.IOS模拟器第一次打开需要进入“设置”中关掉"Auto-Capitalization"选项. 2.NSInteger转化 NSString类型: [NSString strin ...

  8. linux下文件搜索

    常用: grep -nr "关键字" 搜索当前目录下所有匹配关键字的文件 grep -nr "关键字" *php  搜索当前目录下所有匹配关键字的php文件 f ...

  9. 关于c++中方法名前面的双冒号

    #include "iostream" using namespace std; template <typename T> void swap(T &a, T ...

  10. hdu 5874 Friends and Enemies icpc大连站网络赛 1007 数学

    #include<stdio.h> #include<iostream> #include<algorithm> #include<math.h> #i ...