题目

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.

confused what “{1,#,2,3}” means? > read more on how binary tree is serialized on OJ.

OJ’s Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where ‘#’ signifies a path terminator where no node exists below.

Here’s an example:



The above binary tree is serialized as “{1,2,3,#,#,4,#,#,5}”.

分析

给定整数n,求输入元素为[1,n]时,所构成的全部二叉查找树;

我们都知道二叉查找树的特点,左子树节点值小于根节点,右子树节点值大于根节点。

对于输入[1,n],每个值 i 都可以作为根节点,小于i 的元素构成左子树,大于i 的元素构成右子树。

所以,此题的解决办法为二叉树常用递归。

AC代码

class Solution {
public:
vector<TreeNode*> generateTrees(int n) {
if (n <= 0)
return vector<TreeNode *>(1 , NULL); //对值为 [1 , n]的每个元素都可做二叉查找树的根节点
return generateTrees(1, n);
} //构造根节点[lhs , rhs]的所有二叉查找树
vector<TreeNode *> generateTrees(int lhs, int rhs)
{
if (lhs > rhs)
{
return vector<TreeNode *>(1 , NULL);
} //存储每个查找树的根节点
vector<TreeNode *> ret;
for (int r = lhs; r <= rhs; r++)
{
//[lhs~r-1]间节点作为左子树,[r+1~rhs]间节点作为右子树
vector<TreeNode *> lefts = generateTrees(lhs, r - 1);
vector<TreeNode *> rights = generateTrees(r + 1, rhs); //链接符合要求的左右子树
int lsize = lefts.size();
int rsize = rights.size();
for (int i = 0; i < lsize; ++i)
{
for (int j = 0; j < rsize; ++j)
{
//当前节点作为根节点
TreeNode *root = new TreeNode(r);
root->left = lefts[i];
root->right = rights[j];
ret.push_back(root);
}//for
}//for
}//for
return ret;
}
};

GitHub测试程序源码

LeetCode(95) Unique Binary Search Trees II的更多相关文章

  1. LeetCode(96) Unique Binary Search Trees

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

  2. LeetCode(96)Unique Binary Search Trees

    题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...

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

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

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

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

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

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

随机推荐

  1. 改变滚动条的原始样式: chrome 可以改变, IE只能变相关颜色,firfox好像也不好改。最好是自己写一个或是用插件

    相关作者链接地址: https://www.lyblog.net/detail/314.html 问题: 1.我在项目中遇到的问题: 在设置了::-webkit-scrollbar 后,滚动条不见了! ...

  2. 我的NopCommerce之旅(1): 系统综述

    一.概述 NopCommerce是一个开源的购物网站,它的特点是Pluggable modular/layered architecture(可插拔模块分层架构) 二.功能特色介绍 1.适配手机端 2 ...

  3. AJPFX简述i=i+1与i+=1及x++的区别和效率

    i=i+1与i+=1及x++的区别和效率 1.x=x+1,x+=1及x++的效率哪个最高?为什么? x=x+1最低,因为它的执行如下. (1)读取右x的地址: (2)x+1: (3)读取左x的地址: ...

  4. AJPFX关于Collection接口的总结

    ###15Collection-List-ArrayList/LinkedList/*  * Collection接口中的方法* A:添加功能*                 boolean add ...

  5. Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

    Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

  6. C# 一维数组 冒泡排序

    假设有个三个杯子    一个杯子中有一个紫色的乒乓球  一个没有  一个有红色乒乓球    杯子不能动 怎么把紫色和红色的调换呢 主要是先把紫色的放到空的杯子   在把红的放到紫色原来的杯子   再把 ...

  7. 新萝卜家园GHOST WIN7系统3专业装机版

    系统来自系统妈:http://www.xitongma.com/ 系统概述 萝卜家园GHOST win7 64位装机旗舰版加快“网上邻居”共享速度:取消不需要的网络服务组件,系统支持Windows安装 ...

  8. POJ 1739 Tony's Tour (插头DP,轮廓线DP)

    题意:给一个n*m的矩阵,其中#是障碍格子,其他则是必走的格子,问从左下角的格子走到右下角的格子有多少种方式. 思路: 注意有可能答案是0,就是障碍格子阻挡住了去路. 插头DP有两种比较常见的表示连通 ...

  9. ABC3D创客项目:国旗

    国旗是一个国家的象征,也是一个民族的骄傲,国旗带给人们的不仅是荣耀,更多的是爱国的情结.看一场天安门的升旗仪式一度成为广大游客去到北京的必有项目,看国旗仪仗队将五星红旗与太阳同时升起,象征着我国充满活 ...

  10. 51nod 1276 1276 岛屿的数量 (很好玩的题目

    题意: 有N个岛连在一起形成了一个大的岛屿,如果海平面上升超过某些岛的高度时,则这个岛会被淹没.原本的大岛屿则会分为多个小岛屿,如果海平面一直上升,则所有岛都会被淹没在水下. 给出N个岛的高度.然后有 ...