1. Unique Binary Search Trees

  题目链接

  题目要求:

  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.

   1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

  题目分析参考自一博文

  把上例的顺序改一下,就可以看出规律了。
  1                1                      2                       3             3
   \                 \                  /      \                  /              / 
   3                  2               1       3               2             1
   /                    \                                       /                \
  2                      3                                   1                    2

  比如,以1为根的树有几个,完全取决于有二个元素的子树有几种。同理,2为根的子树取决于一个元素的子树有几个。以3为根的情况,则与1相同。

  定义Count[i] 为以[0,i]能产生的Unique Binary Tree的数目,

  如果数组为空,毫无疑问,只有一种BST,即空树,
  Count[0] =1

  如果数组仅有一个元素{1},只有一种BST,单个节点
  Count[1] = 1

  如果数组有两个元素{1,2}, 那么有如下两种可能
  1                       2
    \                    /
      2                1
  Count[2] = Count[0] * Count[1]   (1为根的情况)
                     + Count[1] * Count[0]  (2为根的情况)

  再看一遍三个元素的数组,可以发现BST的取值方式如下:
  Count[3] = Count[0]*Count[2]  (1为根的情况)
                 + Count[1]*Count[1]  (2为根的情况)
                 + Count[2]*Count[0]  (3为根的情况)

  所以,由此观察,可以得出Count的递推公式为
  Count[i] = ∑ Count[0...k] * [ k+1....i]     0<=k<i-1
  问题至此划归为一维动态规划。

  程序如下:

 class Solution {
public:
int numTrees(int n) {
if(n <= )
return ; vector<int> dp(n + , );
dp[] = ;
dp[] = ;
for(int i = ; i < n + ; i++)
for(int j = ; j < i; j++)
dp[i] += dp[j] * dp[i - j - ]; return dp[n];
}
};

LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II的更多相关文章

  1. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  2. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

  3. Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)

    Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...

  4. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  5. Leetcode之二分法专题-704. 二分查找(Binary Search)

    Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 t ...

  6. 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree

    1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...

  7. [leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

    [leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree 链接 leetcode 描述    ...

  8. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...

  10. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

随机推荐

  1. Vc2015 utf8 格式出错

    Vc2015 utf8 格式出错(金庆的专栏)用Vc2015新建一个项目,如下添加一行中文注释.然后将这个文件改为utf8无BOM格式,再转换行结束符为Unix格式.#include "st ...

  2. The packages can be overrided by Java Endorsed Standards

     Endorsed Standards APIs The Endorsed Standards for Java SE constitute all classes and interfaces ...

  3. 剑指Offer——完美+今日头条笔试题+知识点总结

    剑指Offer--完美+今日头条笔试题+知识点总结 情景回顾 时间:2016.9.28 16:00-18:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:完美世界笔试 ...

  4. 视频编码器评测系统:VideoCodecRank

    视频编码器领域一直有个比较复杂的问题:mpeg2.divx.xvid.mpeg4.vp8.vp9.x264.openh264.x265等等这一系列编码器到底哪个好?而对于同一种视频编码器,又包括了各种 ...

  5. JDBC编程-事务编程(四)

    事务的概念 事务的概念在我看来是指的是一组sql序列,这个序列是一块执行的单位,要么全部执行,要不全部执行,这样可以很好的对数据库进行并发控制. 因为数据库是多个用户都可以同时操作的,如果多个用户同时 ...

  6. Android反编译 -- 错误代码还原

    PS:如果阅读体验不好,可以尝试Github版 (<-点左边) 1. setColor(-16777216) 反编译的代码中会有很多setColor(int)的情况,比如setColor(-16 ...

  7. 插件开发之360 DroidPlugin源码分析(二)Hook机制

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52124397 前言:新插件的开发,可以说是为插件开发者带来了福音,虽然还很多坑要填补, ...

  8. TCP协议三次握手与四次挥手详解

    在计算机网络的学习中TCPi协议与Http协议是我们必须掌握的内容,其中Tcp协议属于传输层,而Http协议属于应用层,本博客主要讲解Tcp协议中的三次握手与四次挥手,关于Http协议感兴趣的可以参看 ...

  9. 使用maven将项目打成jar包

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. Cocos2D:塔防游戏制作之旅(八)

    如果所有东西通过检查,则创建一个新炮塔,将它放置在基座上,然后添加到towers数组中. 注意:在方法最后的bridge语法需要做一些解释.你下载的初始项目已经为一 些文件打开ARC,但不是Cocos ...