给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。

示例:

输入: 3

输出: [   [1,null,3,2],   [3,2,null,1],   [3,1,null,null,2],   [2,1,3],   [1,null,2,null,3] ]

重建树一般都是递归求解

  1. class Solution {
  2. public:
  3. vector<TreeNode*> generateTrees(int n)
  4. {
  5. if(n == 0)
  6. return {};
  7. return GetAns(1, n);
  8. }
  9. vector<TreeNode*> GetAns(int start, int end)
  10. {
  11. vector<TreeNode*> subTree;
  12. if(start > end)
  13. {
  14. subTree.push_back(NULL);
  15. }
  16. else
  17. {
  18. for(int i = start; i <= end; i++)
  19. {
  20. vector<TreeNode*> left = GetAns(start, i - 1);
  21. vector<TreeNode*> right = GetAns(i + 1, end);
  22. for(auto l : left)
  23. {
  24. for(auto r : right)
  25. {
  26. TreeNode *root = new TreeNode(i);
  27. root ->left = l;
  28. root ->right = r;
  29. subTree.push_back(root);
  30. }
  31. }
  32. }
  33. }
  34. return subTree;
  35. }
  36. };

Leetcode95. Unique Binary Search Trees II不同的二叉搜索树2的更多相关文章

  1. LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

    题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...

  2. leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法

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

  3. 40.Unique Binary Search Trees(不同的二叉搜索树)

    Level:   Medium 题目描述: Given n, how many structurally unique BST's (binary search trees) that store v ...

  4. LeetCode-95. Unique Binary Search Trees II

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

  5. leetcode95 Unique Binary Search Trees II

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

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

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

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

随机推荐

  1. vue-router配置子路由

    1.改写App.vue 里面的代码 ,增加路由跳转,增加Hi页面1,Hi页面2的跳转 2.修改HI.vue 里面的内容,增加 <router-view class="aaa" ...

  2. Altera FPGA– Bit Slip

    通过在接收端加延时,在延时间隙插入'0'或'1',以使最终接收和期望数据一致. BitSlip操作要注意几点: 1,BitSlip操作在rx_bitslip的上升沿即开始: 2,BitSlip操作开始 ...

  3. python相关软件安装流程图解——虚拟机操作——复制虚拟机主机——CentOS-7-x86_64-DVD-1810

    请先确保已经安装了虚拟机 python相关软件安装流程图解——虚拟机安装——CentOS-7-x86_64-DVD-1810——CentOS-01下载 https://www.cnblogs.com/ ...

  4. vue cli2.x配置多环境打包

    一.安装 npm install --save-dev cross-env 二.配置步骤 1.修改config下的文件 //test.env.js 'use strict' module.export ...

  5. clientHeight / scrollHeight / offsetHeight 等属性的区别图

    网页(内容)可见区域宽:document.body.clientWidth 网页(内容)可见区域高:document.body.clientHeight 即页面浏览器中可以看到内容的这个区域的高度,一 ...

  6. MVC到底是设计模式还是一种框架还是一种架构? https://www.zhihu.com/question/31079945

    具体知乎讨论内容:https://www.zhihu.com/question/31079945 MVC到底是设计模式还是一种框架还是一种架构? 我认为它是3种设计模式的演变和组合:观察者模式(Obs ...

  7. Spring的refresh()方法相关异常

    如果是经常使用Spring,特别有自己新建ApplicationContext对象的经历的人,肯定见过这么几条异常消息:1.LifecycleProcessor not initialized - c ...

  8. 第二十篇:记下第一个mysql触发器

    项目背景:给一个服务限制访问次数,当用户访问这个服务的次数达到这个值的时候,关闭他的访问权限首先访问信息存在一张表中,记录用户的ip:visitor_ip,服务的id:service_id,访问次数: ...

  9. 【LGP5349】幂

    题目 比较厉害的题目了 求 \[\sum_{i=0}^{\infty}\sum_{j=0}^nf_ji^jr^i\] 改变一下求和顺序 \[\sum_{j=0}f_j\sum_{i=0}^{\inft ...

  10. VMware Workstation 10 简体中文安装教程

    分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 分享到 一键分享 QQ ...