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)结构的总数和存储的n个有序数没有关系。即存放的是1,2,3或5、6、9结果都是一样的;
2)n=0\1时,结果都为1;n个有序数。取不论什么一个作为根结点,该值左边的为左子树。右边的为右子树,
在这样的情况下。可能的结构总数为:左子树结构数*右子树结构数。所以原问题就被分解成了两个子问题;
3)将取n个有序数作为根结点的情况的结构问题相加。就可以得到原问题的解,但应注意的是,如:1、2、3
取1或3作为根结点的情况,结构数是同样的,即具有对称性;那么也要考虑到奇偶性;
4)可以被分解成同样的子问题,自己就行採用递归,本程序採用循环的方法。
*/
int numTrees(int n)
{
vector<int> store;
store.push_back(1);
store.push_back(1);
for(int i=2;i<=n;++i)
{
int tmp=0;
for(int j=0;j<i/2;++j)
{
tmp+=2*store[i-1-j]*store[j];
}
if(i%2)
tmp+=store[i/2]*store[i/2]; store.push_back(tmp);
}
return store[n];
}

leetCode(26):Unique Binary Search Trees的更多相关文章

  1. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

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

  2. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  3. Java for LeetCode 095 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 唯一二叉搜索树 II

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

  5. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

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

  6. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

  7. [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树

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

  8. [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

  9. 【leetcode】Unique Binary Search Trees

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

随机推荐

  1. 3 SQL 聚合与排序

    3 聚合与排序 3-1 对表进行聚合查询 聚合函数 通过SQL对数据进行 操作或计算时需要使用函数. 计算表中全部数据行数时,可以使用COUNT函数. COUNT : 计算表中的记录数(行数). SU ...

  2. Centos 7 编译nginx 1.14.0

    步骤一:下载nginx安装包 wget https://nginx.org/download/nginx-1.14.0.tar.gz 步骤二:安装nginx依赖包 yum install -y gcc ...

  3. Linux文件查找find和locate

    目 录 第1章 locate文件查找    1 1.1 概述    1 1.2 locate文件查找的特性    1 第2章 文件查找概述    1 第3章    1 3.1 文件名查找    1 3 ...

  4. LeetCode (32) Longest Valid Parentheses

    题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  5. POJ 3660 Cow Contest(求图的传递性)

    题意: 给定n头牛, 然后有m个比较, 求出有多少头牛能确定自己的排名. 分析: 假设有一头牛a, 有ki头牛强于自己, kj头牛弱于自己, ki + kj == n-1时, 那么这头牛的排名就确定了 ...

  6. 用SQLLDR来装载date类型的控制文件

    以前给山东某单位做oracle数据库恢复得时候,恢复出来得数据中包含date类型,当时给客户提供得是sqlldr得方式,因为数据量比较大,用sqlldr装载起来速度比较快,所以采用了这种方式,结果在装 ...

  7. msp430入门编程36

    msp430中C语言的可移植--面向接口实现

  8. Codeforces 549C(博弈)

    C. The Game Of Parity time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  10. POJ 3281 [网络流dinic算法模板]

    题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...