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 为根的树的个数,等于左子树的个数乘以右子树的个数,左子树是 0 个元素的树,右子树是 2 个元素的树。以 2 为根的树的个数,等于左子树的个数乘以右子树的个数,左子树是1个元素的树,右子树也是1个元素的树,以此类推。当数组为 1,2,3,...,n 时,基于以下原则的构建的BST树具有唯一性:以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1,n]构成。

定义f(i)为以[1,i] 能产生的 UniqueBinarySearchTree 的数目,则有

如果数组为空,毫无疑问,只有一种 BST,即空树,f(0) = 1。

如果数组仅有一个元素 1,只有一种 BST,单个节点,f(1) = 1。

如果数组有两个元素 1,2,那么有如下两种可能

1            2

\        /

2    1

f(2) =    f(0)∗f(1) ,1 为根的情况

+ f(1)∗f(0) ,2 为根的情况

再看一看 3 个元素的数组,可以发现 BST 的取值方式如下:

f(3) =   f(0)∗f(2) ,1 为根的情况

+ f(1)∗f(1) ,2 为根的情况

+ f(2)∗f(0) ,3 为根的情况

所以,由此观察,可以得出f的递推公式为

f(i) =∑f(k−1)×f(i−k)

因此,可以利用动态规划解决此问题:

/*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
*/
//#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <climits>
#include <ctime>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cstdlib>
#include <windows.h>
#include <string>
#include <cstring> using namespace std;
class Solution {
public:
int numTrees(int n) {
vector<int>f(n+1,0);
f[0] = 1;
f[1] = 1;
for(int i=2; i<=n; i++){
for(int j=1; j<=i; j++){
f[i] +=f[j-1]*f[i-j];
}
}
return f[n];
}
}; int main(){
Solution s;
cout<<s.numTrees(3)<<endl;
system("pause");
return 0;
}

  

【leetcode】Unique Binary Search Trees (#96)的更多相关文章

  1. 【leetcode】Unique Binary Search Trees

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

  2. 【leetcode】Unique Binary Search Trees II

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

  3. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...

  4. 【leetcode】 Unique Binary Search Trees II (middle)☆

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

  5. 【题解】【BST】【Leetcode】Unique Binary Search Trees

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

  6. 【leetcode】 Unique Binary Search Trees (middle)☆

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. 【Leetcode】【Medium】Unique Binary Search Trees

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

  8. 【Leetcode】【Medium】Unique Binary Search Trees II

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

  9. 【Leetcod】Unique Binary Search Trees II

    给定结点数n,结点值为1,2,...,n,求由这些结点可以构成的所有二叉查找树. Given n, generate all structurally unique BST's (binary sea ...

随机推荐

  1. 回顾yii的学习进程 总结了一下的发展过程

    如果看到这篇文章,我想说恭喜你 不用,你可以先放下学习yii了  我先学在把结论发给你们节省精力 yii2的学习教程:http://www.digpage.com/recent_update.html ...

  2. 使用antd UI 制作菜单

    antd 主页地址:https://ant.design/docs/react/introduce 在使用过程中,不能照搬antd的组件代码,因为有些并不合适.首先,菜单并没有做跳转功能,仅仅是菜单, ...

  3. Servlet和JSP学习指导与实践(一):Servlet API初探

    前言: JavaSE如何跨度到JavaEE?原本java语言只是专门用于application桌面小应用程序的开发,但自从其追随CGI进入服务器端的开发之后便一发不可收拾.先是Servlet1.0,再 ...

  4. POJ2396 Budget

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7401   Accepted: 2764   Special Judge D ...

  5. UIActivityViewController 系统社交化 共享

    1.UIActivityViewController是继承自UIViewController,是拥有VC的特性 a.初始化 init  , initWithActivityItems:applicat ...

  6. Asynchronous fs.stat.isDirectory()

    function showFile() { for(var i = 0; i< files.length; i++){ var itemFile = files[i]; fs.stat(__di ...

  7. BUAA_OVERWATCH第一次行动前战略部署

    这太IMBA了! 需求调研问卷的反馈 #define A 调查问卷 A设计背景 随着各种新兴手游的兴起,以及各大直播间内Lying Man的火热,以及各种娱乐方式的发展,传统桌游很好地移植到app上的 ...

  8. Day3-python基础3

    本次学习内容 元组 字典 集合 字符编码 文件处理 一.元组 定义:与列表类似,定义是使用() 特性: 1.可存放多个值 2.元组里的元素是不可变的 3.有序,下标从0开始从左往右的顺序访问 元组常用 ...

  9. 网站访问量大 怎样优化mysql数据库

    MySQL优化的一些建议,单机MySQL的优化我分为三个部分,一是服务器物理硬件的优化,二是 MySQL安装时的编译优化,三是自身配置文件my.cnf的优化:如果单机的优化也解决不了你的数据库的压力的 ...

  10. C# byte[]、struct、intptr等的相互转换

    1.struct byte[]互相转换 //struct转换为byte[] public static byte[] StructToBytes(object structObj) { int siz ...