题目

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. PHP之session

    p:first-child, #write > ul:first-child, #write > ol:first-child, #write > pre:first-child, ...

  2. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) B

    Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her s ...

  3. @Results( 中 params 怎么用

    http://blog.csdn.net/z69183787/article/details/16342553 struts2的@Result annotation 如何添加params,并且在页面取 ...

  4. 使用SpringCloud-Netflix

    目录 SpringCloud-Netflix 配置统一依赖管理 创建服务注册中心 创建服务提供者 创建服务消费者 SpringCloud-Netflix Spring Cloud 是一个相对比较新的微 ...

  5. webApi Authentication failed because the remote party has closed the transport stream\身份验证失败了,因为远程方关闭了传输流。

    public class CertificateTrust { public static void SetCertificatePolicy() { //当在浏览器中可以正常访问,而code中出现错 ...

  6. systemback-----做你折腾的后盾

    http://imcn.me/html/y2015/24421.html ubuntu的系统还原工具,最近在学习grunt,要安装nodejs 等一些依赖,对于有轻微系统洁癖的我来说是个好的解决方案, ...

  7. 1102 采药 2005年NOIP全国联赛普及组

    1102 采药 2005年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Description 辰辰是个天资聪颖的孩子 ...

  8. 慢慢积累遇到的linux指令

    sudo 用来以其他身份来执行命令,预设身份为root sudo apt install git 下载git sudo root 进入root身份(只有这个身份ubuntu和window系统之间才能共 ...

  9. Windows系统下Android开发环境搭建

    “工具善其事,必先利其器”.要想学好Android,搭建好Android开发环境是一个良好的开端. Windows系统下Android开发环境主要有4个大的步骤.分别是: 1.JDK的安装 2.ecl ...

  10. COGS 36. 求和问题

    时间限制:1.2 s   内存限制:128 MB [问题描述]     在一个长度为n的整数数列中取出连续的若干个数,并求它们的和. [输入格式]     输入由若干行组成,第一行有一个整数n    ...