题意:

  一棵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. 关于java中的编码问题

    ok,今天搞了一天都在探索java字符的编码问题.十分头疼.最后终于得出几点: 1.网上有很多博客说判断一个String的编码的方法是通过如下代码;但其实这个代码完全是错的,用一种编码decode后, ...

  2. 解析Xml文件的三种方式及其特点

    解析Xml文件的三种方式 1.Sax解析(simple api  for xml) 使用流式处理的方式,它并不记录所读内容的相关信息.它是一种以事件为驱动的XML API,解析速度快,占用内存少.使用 ...

  3. The Largest Generation (25)(BFS)(PAT甲级)

    #include<bits/stdc++.h>using namespace std;int n,m,l,t;int a[1307][137][67];int vis[1307][137] ...

  4. Trie[字典树] 数据结构及基本操作集

    #include <iostream> #include <stdio.h> #include <cstring> #include <algorithm&g ...

  5. PHP 预定义常量(魔术常量)

    显示当前代码在多少行__LINE__ echo __LINE__; 获取当前文件绝对路径 __FILE__ echo __FILE__; //结果为: // D:\xxxx\xxxx\xxxx\ind ...

  6. MCP|LQD|Data-independent acquisition improves quantitative cross-linking mass spectrometry (DIA方法可提升交联质谱定量分析)

    文献名:Data-independent acquisition improves quantitative cross-linking mass spectrometry (DIA方法可提升定量交联 ...

  7. 你的php

    最开始学习做网页,用的是HTML,现在开始php了,那么要想用php,首先你得安装对不对,那么问题来了,你上哪安去啊(一看小编就是东北人),那么小编给各位提供了两个链接(不要告诉其他人哦)https: ...

  8. python依赖文件

    生成 pip freeze >requirements.txt 安装 pip install -r requirements.txt

  9. Flask&&人工智能AI --1

    Flask初识,Response三剑客,jsonify以及send_file.Request,模板语言 Jinja2,用户登录例子,内置Sessio 一.Flask初识 首先,要看你学没学过Djang ...

  10. POJ - 3461 (kmp)

    题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...