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

Example:

  1. Input: 3
  2. Output:
  3. [
  4.   [1,null,3,2],
  5.   [3,2,null,1],
  6.   [3,1,null,null,2],
  7.   [2,1,3],
  8.   [1,null,2,null,3]
  9. ]
  10. Explanation:
  11. The above output corresponds to the 5 unique BST's shown below:
  12.  
  13. 1 3 3 2 1
  14. \ / / / \ \
  15. 3 2 1 1 3 2
  16. / / \ \
  17. 2 1 2 3

这道题是之前的 Unique Binary Search Trees 的延伸,之前那个只要求算出所有不同的二叉搜索树的个数,这道题让把那些二叉树都建立出来。这种建树问题一般来说都是用递归来解,这道题也不例外,划分左右子树,递归构造。这个其实是用到了大名鼎鼎的分治法 Divide and Conquer,类似的题目还有之前的那道 Different Ways to Add Parentheses 用的方法一样,用递归来解,划分左右两个子数组,递归构造。刚开始时,将区间 [1, n] 当作一个整体,然后需要将其中的每个数字都当作根结点,其划分开了左右两个子区间,然后分别调用递归函数,会得到两个结点数组,接下来要做的就是从这两个数组中每次各取一个结点,当作当前根结点的左右子结点,然后将根结点加入结果 res 数组中即可,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. vector<TreeNode*> generateTrees(int n) {
  4. if (n == ) return {};
  5. return helper(, n);
  6. }
  7. vector<TreeNode*> helper(int start, int end) {
  8. if (start > end) return {nullptr};
  9. vector<TreeNode*> res;
  10. for (int i = start; i <= end; ++i) {
  11. auto left = helper(start, i - ), right = helper(i + , end);
  12. for (auto a : left) {
  13. for (auto b : right) {
  14. TreeNode *node = new TreeNode(i);
  15. node->left = a;
  16. node->right = b;
  17. res.push_back(node);
  18. }
  19. }
  20. }
  21. return res;
  22. }
  23. };

我们可以使用记忆数组来优化,保存计算过的中间结果,从而避免重复计算。注意这道题的标签有一个是动态规划 Dynamic Programming,其实带记忆数组的递归形式就是 DP 的一种,memo[i][j] 表示在区间 [i, j] 范围内可以生成的所有 BST 的根结点,所以 memo 必须是一个三维数组,这样在递归函数中,就可以去 memo 中查找当前的区间是否已经计算过了,是的话,直接返回 memo 中的数组,否则就按之前的方法去计算,最后计算好了之后要更新 memo 数组,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. vector<TreeNode*> generateTrees(int n) {
  4. if (n == ) return {};
  5. vector<vector<vector<TreeNode*>>> memo(n, vector<vector<TreeNode*>>(n));
  6. return helper(, n, memo);
  7. }
  8. vector<TreeNode*> helper(int start, int end, vector<vector<vector<TreeNode*>>>& memo) {
  9. if (start > end) return {nullptr};
  10. if (!memo[start - ][end - ].empty()) return memo[start - ][end - ];
  11. vector<TreeNode*> res;
  12. for (int i = start; i <= end; ++i) {
  13. auto left = helper(start, i - , memo), right = helper(i + , end, memo);
  14. for (auto a : left) {
  15. for (auto b : right) {
  16. TreeNode *node = new TreeNode(i);
  17. node->left = a;
  18. node->right = b;
  19. res.push_back(node);
  20. }
  21. }
  22. }
  23. return memo[start - ][end - ] = res;
  24. }
  25. };

Github 同步地址:

https://github.com/grandyang/leetcode/issues/95

类似题目:

Unique Binary Search Trees

Different Ways to Add Parentheses

参考资料:

https://leetcode.com/problems/unique-binary-search-trees-ii/

https://leetcode.com/problems/unique-binary-search-trees-ii/discuss/31494/A-simple-recursive-solution

https://leetcode.com/problems/unique-binary-search-trees-ii/discuss/31535/20ms-C%2B%2B-top-down-DP-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二的更多相关文章

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

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

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

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

  3. LeetCode - Unique Binary Search Trees II

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

  4. [Leetcode] Unique binary search trees ii 唯一二叉搜索树

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

  5. LeetCode——Unique Binary Search Trees II

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

  6. [LeetCode] Unique Binary Search Trees II dfs 深度搜索

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

  7. [leetcode]Unique Binary Search Trees II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上 ...

  8. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  9. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. Bootstrap 4-alpha 初体验

    What is Bootstrap? 第一句话就是废话了,作为新时代有理想有节操的开发人员无人不知无人不晓.可能就是熟悉程度因为各种原因不尽相同,有人只是知道他大概是个什么东西,有些人可能基本可以使用 ...

  2. 从架构层面谈web加载优化(个人整理)

    最近听了阿里一位大牛的讲座,讲web架构优化对网页加载的影响,看完之后对他所讲的一些优化方法进行一些总结和整理,发现收获还是蛮多的,下面多为个人整理和个人见解,希望有说的不对的,能及时指出 1.DNS ...

  3. 学C#之设计模式系列笔记(2)观察者模式

    一.借鉴说明 1.<Head First Design Patterns>(中文名<深入浅出设计模式>) 2.维基百科,观察者模式,https://zh.wikipedia.o ...

  4. 数据结构:堆排序 (python版) 小顶堆实现从大到小排序 | 大顶堆实现从小到大排序

    #!/usr/bin/env python # -*- coding:utf-8 -*- ''' Author: Minion-Xu 小堆序实现从大到小排序,大堆序实现从小到大排序 重点的地方:小堆序 ...

  5. 【Effective Java】9、使用EnumMap代替序数索引

    package cn.xf.cp.ch02.item33; import java.util.EnumMap; import java.util.HashSet; import java.util.M ...

  6. SpringAOP与Redis搭建缓存

    近期项目查询数据库太慢,持久层也没有开启二级缓存,现希望采用Redis作为缓存.为了不改写原来代码,在此采用AOP+Redis实现. 目前由于项目需要,只需要做查询部分: 数据查询时每次都需要从数据库 ...

  7. COLLATE匹配两表数据

    MesOrd.MesNO COLLATE Chinese_Taiwan_Stroke_CI_AS = ErpSO.SoNO

  8. 谈谈我对前端组件化中“组件”的理解,顺带写个Vue与React的demo

    前言 前端已经过了单兵作战的时代了,现在一个稍微复杂一点的项目都需要几个人协同开发,一个战略级别的APP的话分工会更细,比如携程: 携程app = 机票频道 + 酒店频道 + 旅游频道 + ..... ...

  9. 强大的observejs

    写在前面 各大MVVM框架的双向绑定太难以观察,很难直观地从业务代码里知道发生了什么,我不是双向绑定的反对者,只是认为双向绑定不应该糅合进底层框架,而应该出现在业务代码中,或者是业务和框架之间的代码上 ...

  10. shift粘滞键后门创建/复原批处理

    创建shift粘滞键后门: 1 c: 2 3 cd \Windows\System32\ 4 5 rename sethc.exe bak_sethc.exe 6 7 xcopy cmd.exe se ...