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

Analysis: Just recursively count the number of left sub tree times number of right sub tree.

class Solution {
public:
int calculate(int n){
int sum = ;
for(int i = ; i< n ;i++ )
sum += tree[i] * tree[n--i] ;
// tree[i] : left subTree tree[n-1-i] " right subTree
return sum ;
}
int numTrees(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
tree.resize(n+,);
for(int i = ; i <= n ; i++)
tree[i] = calculate(i);
return tree[n] ; }
private :
vector<int> tree;
};

LeetCode_Unique Binary Search Trees的更多相关文章

  1. LeetCode_Unique Binary Search Trees II

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

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

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

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

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

  4. 2 Unique Binary Search Trees II_Leetcode

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

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

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

  6. LEETCODE —— Unique Binary Search Trees [动态规划]

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

  7. 【LeetCode】95. Unique Binary Search Trees II

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

  8. Leetcode 86. Unique Binary Search Trees

    本题利用BST的特性来用DP求解.由于BST的性质,所以root左子树的node全部<root.而右子树的node全部>root. 左子树 = [1, j-1], root = j, 右子 ...

  9. Print Common Nodes in Two Binary Search Trees

    Given two Binary Search Trees, find common nodes in them. In other words, find intersection of two B ...

随机推荐

  1. CCI_chapter 3 Stacks and Queues

    3.1Describe how you could use a single array to implement three stacks for stack 1, we will use [0, ...

  2. 瞬态抑制二极管TVS的基本知识

    二极管是大家熟悉的元件,但瞬态抑制二极管就可能不太熟悉了.本文将介绍这种特殊二极管的用途. 工作原理等基本知识.各种电子设备中的各种半导体器件,一般都在直流低电压范围各作;如果在电源中串入一个瞬间上百 ...

  3. 解决MVC项目中,静态html 未找到时候,404的跳转

    using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using ...

  4. pmp论坛

    PMP论坛: http://www.px101.com/specialpmp/ http://www.pmp.cn/ http://www.pmptuan.com/ http://www.mypm.n ...

  5. poj 3273 Monthly Expense(二分搜索之最大化最小值)

    Description Farmer John ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) ...

  6. art.dialog

    关闭指定弹出窗: art.dialog({ id: 'hetong' }).close(); 关闭所有的iframe弹出窗,art.dia.close();

  7. html5+css3中的background: -moz-linear-gradient 用法 (转载)

    转载至-->http://www.cnblogs.com/smile-ls/archive/2013/06/03/3115599.html 在CSS中background: -moz-linea ...

  8. 由mysql数据库基础上的php程序实现单词的查询、删除、更改和查询

    我做了一个php程序,将表单数据添加到数据库,借用mysql扩展库函数实现对mysql数据库的操作,能够实现添加单词.删除单词.更新和查询单词.运行环境是普通的mysql数据库和php.Apache服 ...

  9. 不可视对象的自己主动实例化BUG

    PB有个隐藏BUG会占用内存.影响效率. 先来做个样例吧 (1)创建一个不可视对象n_base,勾选Autolnstantiate属性 初始化事件constructor里面写messagebox('c ...

  10. Eclipse 将projectBuild Path中引用的jar包自己主动复制到WEB-INF下的lib目录下

    在用用 Eclipse进行Java Web开发时,web应用中引用的jar须要复制到WEB-INF下的lib目录下,否则常常出现ClassNotFound异常. 通过以下方法,能够不用手动拷贝jar包 ...