题目

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. Linux —— 常用命令集合

    关机和重启命令 shutdown [选项] 时间 (添加&,把关机任务放在后台执行) 只有shutdown可以保存关机时资源 操作选项 重启: r 关机: h 取消一个关机任务: c 关机命令 ...

  2. Java三种技术架构

    http://blog.csdn.net/weixin_36416990/article/details/52845868

  3. 计算机中如何实现除数是2的幂次的除法【转载自CSDN】

    前言: 本来是在看汇编里面的数据条件传送指令,做习题的时候看着这么一道有关于2的幂次方除法的题目.结果傻眼了,又尼玛不会了.........第二章看的时候就稀里糊涂的,看了几遍也没看太懂,这回又涉及到 ...

  4. Spring之WebContext不使用web.xml启动 初始化重要的类源码分析(Servlet3.0以上的)

    入口: org.springframework.web.SpringServletContainerInitializer implements ServletContainerInitializer ...

  5. 初始Activity启动模式

    之前断断续续接触了解过Android activity,可是从没有应用过,这次因为一个严重缺陷再次认识Activity的启动模式,相比以前理解更深入了,以后使用检查也就更方便了. 任务栈(Task S ...

  6. (C#)asp_net调试错误解决方法收集(1)

    (C#)asp_net调试错误解决方法收集(1) 2007-11-2309:20 一.异常详细信息:System.InvalidOperationException:对于不返回任何键列信息的Selec ...

  7. dbf 工程模式连接(vfp c# )

    首先现在微软官网下载“Microsoft OLE DB Provider for Visual FoxPro 9.0”驱动 下载完成后得到“VFPOLEDBSetup.msi” 双击安装即可在“C:\ ...

  8. 获取文件的MD5码(C#)

    using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Test ...

  9. 【Linux】Tmux分屏

    1.Tmux Arch维基: https://wiki.archlinux.org/index.php/Tmux_(简体中文) 官方WIKI: https://github.com/tmux/tmux ...

  10. Genymotion的安装与设置

    Genymotion是一款非常好用的虚拟机,利用它可以在window.Liunx或MAC系统上实现Android的模似器.对于开发人员来说,有了Android模似器,就可以在电脑上实时调试安卓app, ...