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. thinkphp 3.2响应头 x-powered-by 修改

    起初是看到千图网的登录链接 查看到的 自己做的网站也看了下 修改的办法就是TP3.2.2 的框架里 具体路径是D:\www\ThinkPHP\Library\Think\View.class.php ...

  2. MySQL数据导出与导入

    工具 mysql mysqldump 应用举例 导出 导出全库备份到本地的目录 mysqldump -u$USER -p$PASSWD -h127.0.0.1 -P3306 --routines -- ...

  3. Python之路【第三篇补充】:Python基础(三)

    参考老师:http://www.cnblogs.com/wupeiqi lambda表达式 学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: # 普通条件语句 if 1 ...

  4. python 文件包含

    Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行.要包含目录里 ...

  5. 单选框的回显c:if

    <input type="radio" name="sex" value="boy" <c:if test="${te ...

  6. QQ空间HD(1)-UIPopoverController基本使用

    UIPopoverController 是iPad的专属API ViewController.m #import "ViewController.h" #import " ...

  7. linux环境下给文件加密/解密的方法

      原文地址:linix环境下给文件加密/解密的方法 作者:oracunix 一. 利用 vim/vi 加密:优点:加密后,如果不知道密码,就看不到明文,包括root用户也看不了:缺点:很明显让别人知 ...

  8. Linux静态库生成指南

    Linux静态库生成指南   Linux上的静态库,其实是目标文件的归档文件.在Linux上创建静态库的步骤如下: 写源文件,通过 gcc -c xxx.c 生成目标文件. 用 ar 归档目标文件,生 ...

  9. vim 中 也可以 直接安装 emmet 直接使用zen coding 生成 l指定个数的 lorem ipsum文字.

    超链接的写法: 当作为链接的文字, 比较长时, 整个作为链接 就显得不是 很适合. 可以取其中的某一个单词 作为 超链接的 关键字:如: click here to continue emmet中如何 ...

  10. 关于Windows下安装mongodb和加入Windows系统启动项

    .首先:在http://www.mongodb.org/downloads官网下载最新的win版本的mongodb下载包(我下载到d盘) .加压缩,修改文件夹名字为mongodb,建立放数据库文件夹w ...