Question

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 

Solution

Key to the solution is to divide the problem into two sub-problems. Unlike "Unique Binary Search Trees", this problem is NP-hard.

 /**
* 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> generateTrees(int n) {
return generateTreesHelper(1, n);
}
private List<TreeNode> generateTreesHelper(int start, int end) {
List<TreeNode> result = new ArrayList<TreeNode>();
if (start > end) {
result.add(null);
return result;
}
for (int i = start; i <= end; i++) {
List<TreeNode> lefts = generateTreesHelper(start, i - 1);
List<TreeNode> rights = generateTreesHelper(i + 1, end);
for (TreeNode left : lefts) {
for (TreeNode right : rights) {
TreeNode tmp = new TreeNode(i);
tmp.left = left;
tmp.right = right;
result.add(tmp);
}
}
}
return result;
}
}

Unique Binary Search Trees II 解答的更多相关文章

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

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

  2. 【leetcode】Unique Binary Search Trees II

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

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

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

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

  5. Unique Binary Search Trees,Unique Binary Search Trees II

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

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

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

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

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

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

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

随机推荐

  1. mysql 中文配置(转)

    Dos下连接mysql后,运行一下几项就可以插入中文了: SET character_set_client = gbk; SET character_set_connection = gbk; SET ...

  2. 使用Windows USB-DVD制作U盘启动安装系统盘

    第一步:到如下所示的地址下载所需要的*.iso系统镜像文件. http://msdn.itellyou.cn/ 第二步:下载Windows USB-DVD工具 https://www.microsof ...

  3. python高级编程之选择好名称:pepe8和命名最佳实践

    # # -*- coding: utf-8 -*- # # python:2.x # __author__ = 'Administrator' # my_list=['a','b','c','d'] ...

  4. 平时的笔记02:处理mp3

    #! /usr/bin/env python # # mutagen aims to be an all purpose media tagging library # Copyright (C) 2 ...

  5. vim 快捷键大全

    一.移动光标 1.左移h.右移l.下移j.上移k 2.向下翻页ctrl + f,向上翻页ctrl + b 3.向下翻半页ctrl + d,向上翻半页ctrl + u 4.移动到行尾$,移动到行首0(数 ...

  6. flexible.js字体大小诡异现象解析及解决方案

    最近在做一个手机端页面时,遇到了一个奇怪的问题:字体的显示大小,与在CSS中指定的大小不一致.大家可以查看这个Demo(记得打开Chrome DevTools). 就如上图所示,你可以发现,原本指定的 ...

  7. linux 系统下配置安装 java jdk 图文流程

    先查看一下系统版本,本例采用的操作系统是CentOS 6.5: 如果你是初装之后的操作系统,那么有可能wget这个组件是不存在的,所以你要安装一下它,这样才可以让你从网上down下你要的安装包: 上面 ...

  8. 关于Http协议(2)--转载

    原文链接:http://www.cnblogs.com/mcad/ HTTP工作原理图 请求报文 1.请求报文长什么样?  Chrome核心的请求报文 2.报文结构 3.报文头部每个字段的意义 //从 ...

  9. jquery滚动到指定元素,模仿锚点

    html <div class="pd-nav"> <div class="n-item active"> 保险服务 <i> ...

  10. UNION ALL

    select field1,field2,field3,field4 from table1 where ... UNION ALL select field1,field2,field3,field ...