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. 1 3 3 2 1
  2. \ / / / \ \
  3. 3 2 1 1 3 2
  4. / / \ \
  5. 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.

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public List<TreeNode> generateTrees(int n) {
  12. return generateTreesHelper(1, n);
  13. }
  14. private List<TreeNode> generateTreesHelper(int start, int end) {
  15. List<TreeNode> result = new ArrayList<TreeNode>();
  16. if (start > end) {
  17. result.add(null);
  18. return result;
  19. }
  20. for (int i = start; i <= end; i++) {
  21. List<TreeNode> lefts = generateTreesHelper(start, i - 1);
  22. List<TreeNode> rights = generateTreesHelper(i + 1, end);
  23. for (TreeNode left : lefts) {
  24. for (TreeNode right : rights) {
  25. TreeNode tmp = new TreeNode(i);
  26. tmp.left = left;
  27. tmp.right = right;
  28. result.add(tmp);
  29. }
  30. }
  31. }
  32. return result;
  33. }
  34. }

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. 杭电1142(最短路径+dfs)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  2. Axure设计分析作业-实例解析

    本文转载自人人都谁产品经理,作者完全使用Axure做了这一个产品需求文档.文档地址:http://1passwordmanager.sinaapp.com/ 大家可以先睹为快.这个PRD完全使用axu ...

  3. win环境下mysql5.6.14的所有变量的默认值

    在windows mysql5.6.14 x64版本下my.ini如下: [mysqld] port  = 3306 socket  = /tmp/mysql.sock basedir=D:/wamp ...

  4. android 缓存Bitmap - 开发文档翻译

    由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Loading a single bitmap into your user interf ...

  5. 学艺不精,又被shell的管道给坑了

    我用过bash shell,而且时间不短了.但我从来没学过shell,至少没有像C++这么认真去学.平时写些基本的脚本没问题,不懂也可以google.百度.可在2014最后一天,掉坑里了. 其实脚本也 ...

  6. JavaScript 基础一

    内部: <Script Language="JavaScript" type="text/javascript"> JavaScript代码 < ...

  7. Block内的强引用

    众所周知,当某个对象持有着一个Block的时候,如果在Block内部使用强引用反过来持有这个对象,就会导致引用循环.为了避免引用循环,可以使用__weak修饰符,苹果的官方文档在用代码演示__weak ...

  8. Git 提供篇

    1. Git自动补全 假使你使用命令行工具运行Git命令,那么每次手动输入各种命令是一件很令人厌烦的事情.为了解决这个问题,你可以启用Git的自动补全功能,完成这项工作仅需要几分钟. 为了得到这个脚本 ...

  9. fuser:用文件或者套接口表示进程

    fuser:用文件或者套接口表示进程 作用:fuser命令用文件或者套接口表示进程. 用法:fuser [-a | -s | -c] [-4 | -6] [-n space] [-k [-i] [-s ...

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

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