题目

Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?

For example,

Given n = 3, there are a total of 5 unique BST’s.

分析

此题与上题本质相同,LeetCode 95 Unique Binary Search Trees要求得到全部二叉查找树,打印其层序遍历序列。

而此题,只要求元素 [1,n] 能构成的二叉查找树个数。

这是一个动态规划的题目

  1. 当 n==0 时 , 自然为 0 ;
  2. 当 n==1 时 ,可构成 1 颗 ;
  3. 当 n==2 时 ,可构成 2 颗 ;
  4. 当 n>2 时,任意 [1,n] 中的值都可做为根节点;

    求解方式为:

    声明数组 nums[n+1] 记录 1—n为界限时,可构成的二叉查找树数目,最终返回 nums[n] ;

    对于界限为 r 时,任一 [1,r] 内一元素均可作为根节点,且 [1,i−1] 为当前左子树,[i+1,r] 为当前右子树;

    因为,

    [1,i−1] 可构成的子树数目,即等于 lefts=nums[i−1]

    [i+1,r] 为 r−i 个连续元素,其构成的子树数目等于 rights=nums[r−i]

    故,当 n=r 时,可构成 sum(lefts∗rights)1~r ;

AC代码

class Solution {
public:
int numTrees(int n) {
if (n <= 0)
return 0;
//保存[1,n]每个值对应的二叉查找树个数
vector<int> nums(n+1, 0); //空子树也算一颗
nums[0] = 1; for (int r = 1; r <= n; ++r)
{
//当 n == 1 或者 n == 2时,满足要求的树的个数为n
if (r <= 2)
{
nums[r] = r;
continue;
}//if //对于 [1 , r]之间的每个元素都可作为根节点
for (int i = 1; i <= r; i++)
{
//此时能构成的二叉查找树个数 = [1,i-1]构成的左子树数目 * [i+1 , r]构成的右子树数目
int lefts = nums[i - 1]; //[i+1 , r]为连续的 r-i 个元素,所构成的树数目等于元素[1 , r-i]构成的数目
int rights = nums[r - i]; //
nums[r] += lefts * rights;
}//for
}//for
return nums[n];
}
};

GitHub测试程序源码

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

  1. LeetCode(96)Unique Binary Search Trees

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

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

  3. LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II

    1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...

  4. LeetCode(98) Validate Binary Search Tree

    题目 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined ...

  5. LeetCode(99) Recover Binary Search Tree

    题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...

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

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

  7. 96. Unique Binary Search Trees(I 和 II)

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

  8. 52. leetcode 96. Unique Binary Search Trees

    96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...

  9. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

随机推荐

  1. 086 Partition List 分隔链表

    给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前.你应当保留两个分区中每个节点的初始相对位置.例如,给定1->4->3->2-&g ...

  2. Android实现沉浸式状态栏(必须高逼格)

     情况一:保留状态栏 只是将我们的布局嵌入到状态栏中 方法一:通过设置theme主题 因为 API21 之后(也就是 android 5.0 之后)的状态栏,会默认覆盖一层半透明遮罩.且为了保持4.4 ...

  3. HDU 3359 高斯消元模板题,

    http://acm.hdu.edu.cn/showproblem.php?pid=3359 题目的意思是,由矩阵A生成矩阵B的方法是: 以a[i][j]为中心的,哈曼顿距离不大于dis的数字的总和 ...

  4. Unity Shader入门精要学习笔记 - 第11章 让画面动起来

    转自 冯乐乐的 <Unity Shader入门精要> Unity Shader 中的内置变量 动画效果往往都是把时间添加到一些变量的计算中,以便在时间变化时画面也可以随之变化.Unity ...

  5. QQ免费企业邮箱申请配置

    对于小企业来说,免费的企业邮箱是不错的选择,省去服务器费用和人员维护费用.在这里说一下,qq的免费企业邮箱.如果想搭建自己的企业邮局,请参考:centos extmail postfix nginx ...

  6. myBatis-类型关联

    1.一对多 collection <resultMap id="deptsql" type="Dept"> <id column=" ...

  7. bootstrop媒体对象、面板和Well

    媒体对象   <div class="media">     <div class="media-left">         < ...

  8. 在Window上用cmd创建.htaccess文件

    Windows 图形下不能直接建立空名字的文件,所以没法直接创建.htaccess文件,不过可以通过命令行创建: cd /path/to/your/dir/ type nul>.htaccess ...

  9. link标签的media属性的用法

    <link rel=stylesheet" type="text/css" href="print.css" media="scree ...

  10. cvCanny的参数

    cvCanny 函数功能:采用Canny方法对图像进行边缘检测 函数原型: void cvCanny( const CvArr* image, CvArr* edges, double thresho ...