题目

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. bzoj1492 [NOI2007]货币兑换Cash【cdq分治】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1492 推荐博客:http://www.cnblogs.com/zig-zag/archive ...

  2. python库使用整理

    1. 环境搭建 l  Python安装包:www.python.org l  Microsoft Visual C++ Compiler for Python l  pip(get-pip.py):p ...

  3. 转 查看磁盘IO负载 - 看哪些进程在读写磁盘 以及oracle 异步I/O 和同步I/O

    https://www.cnblogs.com/cloudstorage/archive/2012/11/11/2764623.html #####sample 1: Oracle等待事件db fil ...

  4. mysql 维护添加远程主机访问

    https://www.cnblogs.com/JNUX/p/6936548.html

  5. ZSP12项目的总结

    前言:一款测量仪器做出来容易,想好做好还是需要投入更多的时间和心血. 项目概述:硬件已经定型,在C8051F020基础上的软件开发. 一 关于C8051F单片机:虽然自己整过8051单片机,但那已经是 ...

  6. Access denied for user ''@'localhost' to database 的一个问题

    $conn = new mysqli("127.0.0.1", 'abc', '', DB_DATABASE); 在提供了用户名的情况下,竟然返回错误 说用户提供的用户为空,非常奇 ...

  7. centos7 更换jdk版本

    查看java版本   java -version 如果有java版本(如果没有直接看红色虚线以下的) 输入 rpm -qa | grep java会显示以下几条内容: ******* ******** ...

  8. DataPicker以及TimePicker显示时间和日期(屏幕上显示)

    public class MainActivity extends Activity { private DatePicker date_picker;private TimePicker time_ ...

  9. Spark-水库抽样-根据抽样率确定每个分区的样本大小

    /* * 输入:采样率,待采样的RDD * 输出:每个分区的样本大小(记录数) * 由采样率确定,每个分区的样本大小 */ def findNumPerPartition[T: ClassTag, U ...

  10. threadLocal遇上线程池导致局部变量变化

    这两天一直在查无线app一个诡异的问题,表象是stg的接口返回数据,和线上接口的返回数据不一致. 1.初步判断:有缓存,查看代码后发现缓存时间直邮6分钟,而且同一个接口,其他调用方的返回数据,stg和 ...