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~n的每个节点为根节点,然后递归的利用它左边的节点构造左子树,放在一个list里面;再利用它右边的节点构造右子树,也放在一个list里面;最终枚举两个list里面的左子树和右子树,构建一棵树。

代码如下:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; left = null; right = null; }
* }
*/
public class Solution {
private ArrayList<TreeNode> generate(int start,int end){
ArrayList<TreeNode> answer = new ArrayList<TreeNode>();
if(start > end){
answer.add(null);
return answer;
} //for every node from start to right,make it as tree root and recursively build its left and right child tree
for(int i = start; i <= end;i++){
ArrayList<TreeNode> left = generate(start, i-1);
ArrayList<TreeNode> right = generate(i+1, end);
for(TreeNode l:left){
for(TreeNode r:right){
TreeNode root = new TreeNode(i);
root.left = l;
root.right = r;
answer.add(root);
}
} } return answer; }
public List<TreeNode> generateTrees(int n) {
return generate(1,n);
}
}

【leetcode刷题笔记】Unique Binary Search Trees II的更多相关文章

  1. LeetCode(95) Unique Binary Search Trees II

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

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

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

  3. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

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

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

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

  6. 【LeetCode】95. 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 II

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

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

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

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

  10. Unique Binary Search Trees,Unique Binary Search Trees II

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

随机推荐

  1. 在 Linux 系统下使用 PhotoRec & TestDisk 工具来恢复文件

    当你在系统中有意或无意地使用 shift + delete 组合键.删除选项,或是清空回收站的方式来删除一个文件时,该文件的内容并没有从硬盘(或是其它存储设备)上直接销毁. 它仅仅是从系统的目录结构中 ...

  2. VBA小功能集合-判断列内是否有重复值

    1.判断列内是否有重复值: Dim arrT As Range Dim rng As Range Set arrT = Range("A:A")'判读A列单元格 For Each ...

  3. .Net Framework 与 SQL Server 2005 混乱的时间最大最小值

    Net Framewrok 中,DateTime.MinValue => 0001/01/01 00:00:00SqlDateTime.MinValue.Value  => 1753/01 ...

  4. 让 webpack 加载 Source Map

    在浏览器中运行的 JavaScript 代码都是编译器输出的代码,这些代码的可读性很差.如果在开发过程中遇到一个不知道原因的 Bug,则你可能需要通过断点调试去找出问题. 在编译器输出的代码上进行断点 ...

  5. linux之ftp命令详解

    我们在使用ftp客户端访问到ftp服务器之后,往往需要进行相关操作,比如从远程机器上下载文件,或者将文件传输到远程机器上.需要使用ftp的相关命令,本文讲述了ftp常用的一些操作. 方法/步骤     ...

  6. Harvard数据库课程CS 265: Research Topics in Database Systems

    CS 265: Research Topics in Database Systems Announcements Quiz 3 will be posted. Good luck! Quiz 2 h ...

  7. k8s部署nginx集群

    环境: 两台虚拟机, 10.10.20.203 部署docker.etcd.flannel.kube-apiserver.kube-controller-manager.kube-scheduler ...

  8. sublime使用技巧(3)-- 常用快捷键【持续更新】

    ♥ Ctrl + Shift + v 这样粘贴可以保持原格式,不会有缩进上的困扰 Ctrl + k 用Ctrl + d选中重复单词时跳过当前选中 Ctrl + Enter 在光标所在行的下一行新建一行 ...

  9. excel表格系列

    MicroSoft Excel表格系列问题 1.excel表格修改默认行高 2.[Excel技巧]Excel实现部分保护技巧

  10. 【智力题】IO——行测、笔试、面试中遇到的

    昨天(05.23)下午去参加了明源软件的暑期实习宣讲+笔试,第一次听说这个行业,行业和笔试风格完全不一样啊,5道行测智力题+1个问答+ 斐波那契数列 + 洗牌算法(思想.流程图.代码),今年回来后线上 ...