题意:

  一棵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. Ubuntu14跑DSO

    按照https://github.com/JakobEngel/dso上的说明,make -j4的时候出现一下错误: /home/zhao/dso/src/FullSystem/CoarseIniti ...

  2. Asp.Net 遍历 循环 显示所有COOKIS,SESSION,Applocation

    在C#中循环显示SESSIOn: Response.Write("<br>Session的所有值:<br>"); foreach (string obj i ...

  3. STL特性总述——写在前面

    所谓的容器,见名知意,容纳其他数据的"器具": 特点 1)支持泛型: 2)保存副本:本质上传入对象的拷贝. 3)内存托管 :构建对象于堆,无需人工干预,自动管理内存的生存周期. S ...

  4. C# 、.NET、ASP.NET MVC积累

    2016-10-27 给视图中的select赋值: 控制器: public ActionResult Add() { List<SelectListItem> ClassName = ne ...

  5. [Design-Pattern]工厂模式

    Java版本 1 package interfaces; 2 3 interface Service { 4 void method1(); 5 void method2(); 6 } 7 8 int ...

  6. C 语言实例 - 判断回文数

    C 语言实例 - 判断回文数 判断一个数是否为回文数. 设n是一任意自然数.若将n的各位数字反向排列所得自然数n1与n相等,则称n为一回文数.例如,若n=,则称n为一回文数:但若n=,则n不是回文数 ...

  7. iOS sqlite

    iOS sqlite数据库操作.步骤是: 先加入sqlite开发库libsqlite3.dylib, 新建或打开数据库, 创建数据表, 插入数据, 查询数据并打印 1.新建项目sqliteDemo,添 ...

  8. php模拟post提交数据

    $data = '{ "id": "17999030", "method": "sayHello", "jso ...

  9. 如何自动更新SVN项目

    在桌面新建“SVN Update.bat”文件,把下面的命令复制到该文件中,保存并退出,然后使用windows的“任务计划”功能,就可以实现定时自动更新SVN目录. 按此批处理文件的方法执行,一次可自 ...

  10. Spark 概述

    Spark 是什么? ● 官方文档解释:Apache Spark is a fast and general engine for large-scale data processing. 通俗的理解 ...