题目描述:
给定整数n,计算存储序列为1...n的结构唯一的BST的个数
题目来源
http://oj.leetcode.com/problems/unique-binary-search-trees/
题目分析:
对于一个表示序列为1...n的BST,根元素可以是1到n中的任何一个数,当根元素为 i 时,左子树为表示1...i - 1的BST,右子树为表示i + 1...n的BST,所以,原问题可以通过子问题的解得到
定义状态f(i,j),状态f(i,j)为表示序列i...j的结构唯一的BST的个数,通过枚举根的值可以得到:
f(i,j) = sum(f(i,k - 1) * f(k + 1, j))(i <= k <= j)
时间复杂度:O(n^3)
示例代码:
int dp[][];

int numTrees(int n) {
memset(dp, , sizeof(dp));
for(int i = ; i <= n; ++i)
dp[i][i - ] = dp[i + ][i] = ; for(int len = ; len <= n; ++len) {
for(int i = ; i <= n - len + ; ++i) {
for(int k = i; k <= i + len - ; ++k) {
dp[i][i + len - ] += dp[i][k - ] * dp[k + ][i + len - ];
}
}
} return dp[][n];
}

Unique Binary Search Trees-计算表示相同序列的不同BST个数的更多相关文章

  1. Unique Binary Search Trees I&II——给定n有多少种BST可能、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. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

  4. 【LeetCode】95. Unique Binary Search Trees II

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

  5. 96. Unique Binary Search Trees

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

  6. 52. leetcode 96. Unique Binary Search Trees

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

  7. leetcode -day28 Unique Binary Search Trees I II

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

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

  9. LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

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

随机推荐

  1. 史上最浅显易懂的Git教程3 分支管理

    假设你准备开发一个新功能,但是需要两周才能完成,第一周你写了50%的代码,如果立刻提交,由于代码还没写完,不完整的代码库会导致别人不能干活了.如果等代码全部写完再一次提交,又存在丢失每天进度的巨大风险 ...

  2. UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position xxx ordinal not in range(12

    python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't deco ...

  3. PDO:: 数据访问抽象层 ? :

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. authority分层

     

  5. Servlet单例模式(注意)

    package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax ...

  6. ArcGIS api for js OverviewMap(鹰眼/概览图)

    说明.本篇博客中主要介绍 地图显示在某个div情况 1.运行效果 2.HTML <!DOCTYPE html> <html> <head> <meta htt ...

  7. maven工作的过程

    1 建立各个module之间的依赖关系 2 越底层的依赖的module先生成 3 下载远程库中的依赖 4 先生成本地被依赖的module 问题是,如何保证本次module和远程库中的包不重名?

  8. 我的Java开发学习之旅------>Java String对象作为参数传递的问题解惑

    又是一道面试题,来测试你的Java基础是否牢固. 题目:以下代码的运行结果是? public class TestValue { public static void test(String str) ...

  9. 我的Android进阶之旅------>直接拿来用!最火的Android开源项目

    转载于CSDN,相关链接如下: http://www.csdn.net/article/2013-05-03/2815127-Android-open-source-projects http://w ...

  10. 微软Azure区块链开发工具包三大功能详解

    2018年11月15日,微软宣布了Azure区块链开发工具包,它基于微软的无服务器技术构建,并且利用微软和第三方SaaS,完美集成了区块链.该工具包扩展了微软的区块链开发模板和Azure Blockc ...