题意:

  一棵BST有n个节点,每个节点的key刚好为1~n。问此树有多少种不同形态?

思路:

  提示是动态规划。

  考虑一颗有n个节点的BST和有n-1个节点的BST。从n-1到n只是增加了一个点n,那么点n可以放的地方并不多,而且有一些规律。由于n是最大的,所以必定是在最右边,但是它的上面和下面也可以有一些点,假设点n的上面有k个点,下面则为n-k-1个。观察到点n的上面的点必定是[1, n-k-1],下面的点是[n-k, n-1],而点数<n的BST的形态数都已经求出,那么枚举这个k值就行了。

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

AC代码

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

  1. Unique Binary Search Trees(dp)

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

  2. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

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

  4. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

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

  5. 96. Unique Binary Search Trees (Tree; DP)

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

  6. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  7. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  8. LeetCode - Unique Binary Search Trees II

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  9. Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II

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

随机推荐

  1. App知识点汇总

    1.Fragment 2.AndroidStudio 用夜神安卓模拟器调试 进入夜神模拟器安装目录下的bin目录,执行nox_adb.exe connect 127.0.0.1:62001,Andro ...

  2. streaming kafka direct 详解

    http://blog.cloudera.com/blog/2015/03/exactly-once-spark-streaming-from-apache-kafka/ http://www.jia ...

  3. 【mybatis在控制台打印sql语句配置】

    导入jar包:http://pan.baidu.com/s/1sl8RU37 <strong>############################################### ...

  4. C#在Linux上的开发指南(续)

    续之前的一篇开发指南http://www.cnblogs.com/RainbowInTheSky/p/5496777.html 部分人在部署的时候经常出现dll兼容问题(其实可以看小蝶惊鸿的文章,蝶神 ...

  5. sqlserver2012——触发器

    触发器:是一个修改指定数据时执行的存储过程. 创建触发器 Create Trigger trigger_name ON {table|view} { } 例子: insert触发器: create T ...

  6. Complex复数类——课堂作业

    代码: #include<iostream> #include<cmath> using namespace std; class Complex { public: Comp ...

  7. 关于状态压缩DP以及状态压缩

    首先要明确:状态压缩是利用数字来代表一组序列的方法,从而降低序列访问的复杂度,本质上跟HASH有着差不多的思想,但是其实就是数位运算的一种 定义:集合中共有N个数字,其中每个数字均小于K,能么我们可以 ...

  8. luoguP4931 情侣?给我烧了!(加强版)

    luogu 普通版题解:https://www.cnblogs.com/lcxer/p/10876856.html 在普通版里,我们考虑对于\(n\)对情侣,恰好\(k\)对是和谐的方案数是 \[ a ...

  9. tomcat memecached session 共享同步问题的解决

    事件缘由:一个主项目“图说美物”,另外一个子功能是品牌商的入驻功能,是跟主项目分开的项目,为了共享登录的用户信息,而实现session共享,俩个tomcat,一个tomcat6,一个tomcat7 w ...

  10. 原生JS实现日历

    这周写自己的项目发现又用到日历了,加之自己毕业之后的第一个工作中遇到的任务也是需要写个日历(组员写了,我就不用写了) 今天就来好好折腾一下日历是怎么写的. 首先,我们看看 windows 的日历.发现 ...