题目

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. FTP任务(重点看断点续传)

    一.FTP任务目录: 1. 多用户同时登陆:     socketserver 2. 用户登陆,加密认证: md5加密 3. 上传/下载文件,保证文件一致性:md5摘要 4. 传输过程中现实进度条 5 ...

  2. 转 11g Grid Control: Overview of the EMCTL Options Available for Managing the Agent

    1.概念: The Enterprise Manager DBConsole consists of the following components: - A Standalone OC4J Man ...

  3. Azkaban的功能特点(二)

    Azkaban是什么?(一) 不多说,直接上干货! http://www.cnblogs.com/zlslch/category/938837.html Azkaban的功能特点 它具有如下功能特点: ...

  4. codeforces736D. Permutations(线性代数)

    题意 $m \leqslant 500000$,题目打错了 Sol 神仙题Orz 构造矩阵$B$,使得$B[b[i]][a[i]] = 1$ 那么他的行列式的奇偶性也就对应了生成排列数列数量的奇偶性( ...

  5. iOS上线check_list

    iOS 上线前 check_list 类型 序号 检查项 结果(pass/no) 安装 卸载 1 非越狱环境下的安装.卸载 2 越狱环境下的安装.卸载 3 安装文件检查,无泄漏用户信息的隐患 4 卸载 ...

  6. php 正则符号说明

    preg_match_all ("/<b>(.*)<\/b>/U", $userinfo, $pat_array); preg_match_all (&qu ...

  7. COGS 615. 韩国明星

    [问题描述] 在LazyCat同学的影响下,Roby同学开始听韩国的音乐,并且越来越喜欢H.o.T,尤其喜欢安七炫和Tony,可是,爱学习爱思考的Roby同学想,如果以后喜欢的韩星越来越多怎么办呢?R ...

  8. (一)maven之创建一个maven项目

    为什么要使用Maven? 1.  maven使用的是本地仓库存储jar,所有项目都会共用仓库中的同一份jar. 2.  Spring core.jar必须同时引用版本兼容的common-logging ...

  9. poj1595 水题

    题意:输入n, 和c  统计1 - n 有多少个素数为cnt 若 2*c > cnt 则将素数全部输出 否则分支判断:  若cnt 为偶数,则从中心开始输出2*c 个  若cnt 为奇数,则从中 ...

  10. webStorm Ctrl+s 自动格式化 然后 保存 用宏命令

    使用WebStorm的Macros宏指令,实现保存的同时格式化代码,并跳至行尾 https://blog.csdn.net/gyz718/article/details/70556188