题目

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. python 基础(九) 文件操作

    文件操作 一.函数: f = open(’文件名','打开方式'[,encoding='字符编码']) open 打开的方式 字符 说明 r 只读的方式打开 rb 以二进制的形式打开文件 只读 r+ ...

  2. [已读]JavaScript DOM编程艺术

    看到过很多人将它作为推荐入门书籍,当时我刚看完ppk和javascript精粹,于是看到这本就觉得很一般了.怎么说,它适合基础.

  3. Crusher Django 学习笔记3 学习使用模板系统

    http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial3-using-template.html 顺便学习一下 goag ...

  4. Windows server 2003 + IIS6 搭建Asp.net MVC运行环境

    安装.Net Framework4.0.下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=17718 安装WindowsServ ...

  5. 初始Mybatis,好累,自己感觉自己快坚持不了了

    Mybatis1.持久化 持久化,就是内存数据和硬盘数据状态的转换 2.ORM思想Object Relation Mapping 对象关系映射 3.MyBatis入门案例 3.1导入jar包 依赖 & ...

  6. java面试题(基础部分)

    1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致. 2.Java有 ...

  7. vim编辑器高级应用

    1. vim主要模式介绍 命令模式.命令行模式.编辑模式 字符操作:i 当前插入, I行首插入, a当前字符之后插入,A行首插入, ESC退出当前模式 2. vim命令模式 3. vim插入模式 4. ...

  8. datagrid数据网格获取所有选中行的索引,插入某个列值为其他列的运算值

    获取所有选中行的索引,存入数组ary中: var data=$("#dg").datagrid("getSelections"); var ary=[]; fo ...

  9. 【读书笔记】构建之法(CH4~CH6)

    从chapter4至chapter6,围绕着构建过程的合作讨论构建之法,而合作与个人工作的区别却以一个微妙的问题为开端:阅读别人的代码有多难? 两人合作:(驾驶员与领航员) 合作要注意代码风格规范与设 ...

  10. Xcode 升级后,cocoaPod 问题

    当我从Xcode 6.3切换到Xcode6.4的时候,因为我是mac上安装了两个不同的版本,现在把Xcode 6.3卸掉了. 现在再次运行pod install命令的时候,提示如下错误:   Upda ...