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. JavaWeb学习总结(一)——JavaWeb开发入门

    http://www.cnblogs.com/xdp-gacl/p/3729033.html 只为成功找方法,不为失败找借口! JavaWeb学习总结(一)--JavaWeb开发入门 一.基本概念 1 ...

  2. Python开发【第十四篇】:Web框架本质

    Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  3. Python开发【第六篇】:模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  4. Intellij Idea中运行tomcat 报內存溢出 解决方案 火龙战士 火龙战士

    在Run/Debug configuration 的你要运行行的tomcat里面的 vm options里面输入 -server -XX:PermSize=128M -XX:MaxPermSize=2 ...

  5. 通过CSS实现的html背景色渐变

    实现代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  6. 利用ant脚本 自动构建svn增量/全量 系统程序升级包

    首先请允许我这样说,作为开发或测试,你一定要具备这种 本领.你可以手动打包.部署你的工程,但这不是最好的方法.最好的方式就是全自动化的方式.开发人员提交了代码后,可以自动构建.打包.部署到测试环境. ...

  7. iOS开发 关于SEL的简单总结

    SEL就是对方法的一种包装.包装的SEL类型数据它对应相应的方法地址,找到方法地址就可以调用方法.在内存中每个类的方法都存储在类对象中,每个方法都有一个与之对应的SEL类型的数据,根据一个SEL数据就 ...

  8. Memcached基础知识

    主要内容: Memcached基本的工作原理 Memcached的两阶段哈希 Memcached的数据存储方式 Memcached新建Item分配内存过程 Memcached的数据过期方式 Memca ...

  9. A simple Snippet in ST2

    Reference: http://web-design-weekly.com/2012/07/03/snippets-in-sublime-text-2/ A sample - cofirm (To ...

  10. POJ 2151 Check the difficulty of problems

    以前做过的题目了....补集+DP        Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K ...