问题描述:

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. HDU 5045

    http://acm.hdu.edu.cn/showproblem.php?pid=5045 题意:n个学生m道题,一个n*m的矩阵代表第n个学生解第m题AC的概率,任意两学生做题数差距不能大于1,问 ...

  2. git回滚

    Git回滚的常用手法 07net01.com 发布于 4小时前 评论 传统VCS的回滚操作 对于版本控制系统VCS来说,回滚这个操作应该是个很普通也是很重要的需求. 如果你是传统VCS,比如SVN或者 ...

  3. Send Mail using C# code

    using System.Net.Mail; public class MailHelp { public static void Send(string subject, string body) ...

  4. C# Listview 第一列不能居中

    /********************************************************************** * C# Listview 第一列不能居中 * 说明: ...

  5. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  6. SSIS使用OleDB和Ado.Net两种方式调用 存储过程

    在使用”执行 SQL 任务“组件调用存储过程时,连接方式使用OleDB和Ado.Net稍有不同,结合图例说明一下 当我们使用OleDB时,设置的截图如下: 参数使用?来代替,Parameter Nam ...

  7. 解读vmstat中的ACTIVE/INACTIVE MEMORY

    vmstat 命令能够报告关于内核线程.虚拟内存.磁盘.陷阱和 CPU 活动的统计信息,那么我们又该如何理解其工作原理呢? vmstat -a 命令能看到active memory 和 inactiv ...

  8. 隐马尔科夫模型(HMM)的概念

    定义隐马尔科夫模型可以用一个三元组(π,A,B)来定义:π 表示初始状态概率的向量A =(aij)(隐藏状态的)转移矩阵 P(Xit|Xj(t-1)) t-1时刻是j而t时刻是i的概率B =(bij) ...

  9. Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  10. 解决LinearLayout中控件不能居右对齐

    转载自:http://lgb168.blog.163.com/blog/static/49674438201172492935235/ 2011-08-24 21:35:25|  分类: Androi ...