Unique Binary Search Trees

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个元素来说,f[1]=1;
对于2个元素来说,有两种f[2]=2;
对于3个元素来说,
1) 把1作为根节点,则2,3都连在右边,共有2种
2)把2作为根节点,则1,2必须一个在左边,一个在右边,共1种
3)把3作为根节点,则2,3必须在左边,共有2种
 
把i作为根节点时,我们有前面i-1个元素放在左边,后面i+1到n个元素放在右边
 
 
所以对于f[n]我们有以下的公式:
 
f[n]=f[n-1]+f[1]f[n-2]+f[2]f[n-3]+......+f[n-2]f[1]+f[n-1];
 
引入f[0]=1,f[1]=1;
则f[n]=f[0]f[n-1]+f[1]f[n-2]+f[2]f[n-3]+......+f[n-2]f[1]+f[n-1]f[0];
 
 
 class Solution {
public:
int numTrees(int n) { if(n==||n==)
{
return ;
}
vector<int> f(n+); f[]=;
f[]=; for(int i=;i<=n;i++)
{
f[i]=;
for(int j=;j<i;j++)
{
f[i]+=f[j]*f[i-j-];
}
} return f[n];
}
};

【leetcode】Unique Binary Search Trees的更多相关文章

  1. 【leetcode】Unique Binary Search Trees II

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

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

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

  3. 【leetcode】Unique Binary Search Trees (#96)

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

  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. iOS-马上着手开发iOS应用应用程序-第一部分介绍

    教程:基础 main 中的 main.m 函数会调用自动释放池 (autorelease pool) 中的 UIApplicationMain 函数. @autoreleasepool { retur ...

  2. ehcache memcache redis 三大缓存男高音

    最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考!  Ehcache 在java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...

  3. Jenkins入门总结

    Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目. 2.监控外部调用执行的工作 在网上貌似没有找到Jenkins的中文的太多的文 ...

  4. centos yum 安装

    LINUX下YUM源配置 1.确保RHEL5中已经安装了yum [root@lvs-master ~]# rpm -qa |grep yumyum-metadata-parser-1.1.2-3.el ...

  5. codeforces CF475 ABC 题解

    Bayan 2015 Contest Warm Up http://codeforces.com/contest/475 A - Bayan Bus B - Strongly Connected Ci ...

  6. CString::Mid成员函数

    CString Mid( int nFirst, int nCount ) const; 此成员函数从此CString对象中提取一个长度为nCount个字符的子串,从nFirst(从零开始的索引)指定 ...

  7. mfc 调试 弹消息

    AfxMessageBox(_T("edit change msg")); OutputDebugString(L"jian "); CString str; ...

  8. 2015年12月02日 GitHub入门学习(四)Git操作

    序,学习使用Git是一项新技能,你将了解到Git与SubVersion的区别. 一.基本操作 git init 初始化仓库,请实际建立一个目录并初始化仓库,.git目录里存储着管理当前目录内容所需的仓 ...

  9. IIS7.5 在已有的WEB网站上配置FTP发布

    IIS7.5 有了很多新特性,例如FashCGI,Rewrite 模块的内置,简易的FTP发布等等,但是即使是微软,也没有详细的文档,本文详细的介绍了如何在现有的WEB网站上建立FTP发布. IIS ...

  10. ajax浅析---UpdatePanel

    使用UpdatePanel控件 UpdatePanel可以用来创建丰富的局部更新Web应用程序,它是ASP.NET 2.0 AJAX Extensions中很重要的一个控件,其强大之处在于不用编写任何 ...