问题描述:

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:

遍历+递归

二叉查找树的根节点可以是1~n中的任何一个数i

根为i的二叉树数量=左子树数量*右子树数量

左子树的根节点取值范围为1~i,右子树的根节点取值范围为i+1~n,i+1~n组成的二叉查找树的数量又与1~n-i的相同

代码1:

 class Solution {
int sum;
public:
int numTrees(int n) {
if(n == ) return ;
if(n == ) return ;
for(int i = ; i <= n; i++){
int l = numTrees(i-);
int r = numTrees(n-i);
sum = sum + (l==?:l) * (r==?:r);
}
return sum;
}
};

但是,此方法超时

Q7: Unique Binary Search Trees的更多相关文章

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

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

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

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

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

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

  4. 【leetcode】Unique Binary Search Trees

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

  5. 【leetcode】Unique Binary Search Trees II

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

  6. 41. Unique Binary Search Trees && Unique Binary Search Trees II

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

  7. LeetCode: Unique Binary Search Trees II 解题报告

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

  8. LeetCode - Unique Binary Search Trees II

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  9. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

随机推荐

  1. AFNnetworking入门

    AFNetworking官网入门教程简单翻译,学习 AFNetworking 是一个能够快速使用的ios和mac os x下的网络框架,它是构建在Foundation URL Loading Syst ...

  2. HexDump.java解析

    从包名我们可以看出该类并没有对应用开发者开放,也就是说在google开放的Android API文档中并没有该类的相关介绍:好在Android系统源码是开源的,因此我在解决framework中问题的时 ...

  3. Azure 自动化:使用PowerShell Credential连接到Azure

    最近在中国版windows azure中新上线的自动化功能, 使用自动化,您可以导入自己的PowerShell脚本,然后设置一个运行计划使得脚本能按计划运行. 在本文中,我们来学习如何使用PowerS ...

  4. 如何配置magento免运费商品方法

    作为magento电商来说,免运费是一种常见的促销手段,要让产品成为免运费对magento来说并不难,后台操作即可完成. 首先,我们要建立一个新的产品属性. catalog->attribute ...

  5. js prototype新感悟

    prototype是js的一个原型属性,这个属性可以创建对象属性和方法. 子类继承原型属性时,会继承父类的原型属性和方法. prototype只能作用到类上,不能作用到对象上. ----------- ...

  6. CI中的事物

    $this->db->trans_begin(), 在使用时,设置了  mysql AUTOCOMMIT = 0,表示禁止自动提交, 在未提交和回滚之前,不会提交sql查询. AUTOCO ...

  7. PostgreSQL增删数据命令示例

    在PostgreSQL中如何用简单的几条SQL语句生成大量的测试数据呢? 此处,我简单的写一个例子,供参考(在Postgresql9.1下面做的): (1)生成一万条测试数据的表foo mydb=# ...

  8. linux/lib/string.c

    /** * strlen - Find the length of a string * @s: The string to be sized */ size_t strlen(const char ...

  9. WordPress自定义文章页面模板

    如果想让某个分类的文章页面样式有别于其它分类,我们可以使用自定义的模板的方法实现.例如,我们准备让名称为 WordPress 的分类文章使用有别于其它分类的模板样式, 首先在所用主题根目录新建一个名称 ...

  10. Harris角点(转载)

    1. 不同类型的角点 在现实世界中,角点对应于物体的拐角,道路的十字路口.丁字路口等.从图像分析的角度来定义角点可以有以下两种定义: 角点可以是两个边缘的角点: 角点是邻域内具有两个主方向的特征点: ...