一、     称号

给定的数目n。问:有多少种不同BST(二叉搜索树)

比如:

因为N =3,共同拥有5种独特的BST。

1          3      3       2      1

\        /      /        / \      \

3      2    1     1   3     2

/      /       \                  \

2     1         2                 3

二、分析

要求一定的二叉查找树的数量。我们知道二叉查找树能够随意取根。从处理子问题的角度来看,选取一个结点为根。可把结点分成左右子树,以这个结点为根的可行二叉树数量就是左右子树可行二叉树数量的乘积,所以总数量是将以全部结点为根的可行结果累加起来。

看到有人说到卡特兰数,这正是卡特兰数的一种定义方式。是一个典型的动态规划的定义方式。

卡特兰数的一半公式为Cn= 1/(n+1)*(2n,n) = (2n)!/{(n+1)!*n!}

class Solution {
public:
int numTrees(int n) {
int flag=1;
for(int i=2;i<=n;i++) {
flag=2*(2*i-1)*flag/(i+1);
}
return flag;
}
}; 二: class Solution {
public:
int numTrees(int n)
{
return numTrees(1,n);
} int numTrees(int start, int end)
{
if (start >= end)
return 1; int totalNum = 0;
for (int i=start; i<=end; ++i)
totalNum += numTrees(start,i-1)*numTrees(i+1,end);
return totalNum;
}
}; class Solution {
public:
int numTrees(int n) {
if (n < 0) return 0;
vector<int> trees(n+1, 0);
trees[0] = 1; for(int i = 1; i <= n; i++)
for (int j = 0; j < i; j++)
trees[i] += trees[j] * trees[i-j-1];
return trees[n];
}
};

版权声明:本文博主原创文章,博客,未经同意不得转载。

Leetcode:unique_binary_search_trees的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. 手动安装英特尔® 凌动™ Android* x86 模拟器映像

    android的模拟器实在是太慢了,慢的让人欲仙欲死,欲罢不能.猛然发现我的电脑是intel的CPU,我勒个去,换x86模拟器.然后悲剧了,伟大的gfw 我要装sdk,我要研究android开发,到底 ...

  2. 浅谈http请求数据分析

    前段时间,我一个朋友给我打了个电话.说是现在在搞网络销售,问我能不能帮他整个自动发帖机.说实在的,以前没有弄过这块,我就跟他讲我试试看吧,能不能成不能保证.毕竟是搞程序的嘛,自学的能力还是有滴.经过一 ...

  3. js比量undefined种类

    js比量undefined种类 if (reValue== undefined) {     alert("undefined"); } 发现推断不出来.最后查了下资料要用type ...

  4. Hibernate一个简短的引论

    我们从几个方面进行阐述Hibernate When? What ? How? When? Hibernate由来是因为当时EJBBean1.1在处理entittBean架构时,花费的时间要比业务逻辑很 ...

  5. ext Window点击右上角的关闭(Xbutton)加入监控事件

    使用场景:关闭window的时候添加监听事件. 正确的使用方式: addwin = new Ext.Window({ title : '新增', closable : true, width : 50 ...

  6. nyist 488 素数环(搜索+回溯)

     素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 有一个整数n,把从1到n的数字无反复的排列成环,且使每相邻两个数(包含首尾)的和都为素数,称为素数环. ...

  7. plsql dev中Dynamic Performance Tables not accessible分析解决(转)

    使用plsql dev的朋友多遇到过类此如下面的提示: Dynamic Performance Tables not accessible, Automatic Statistics Disabled ...

  8. Android供TextView添加多个点击文字

    我们使用社会性软件的过程中会或多或少像别人的帖子点,图. : 能够看到用户页面显示出来的仅仅是点了赞的用户的名称,点击这些名称能够进入到该用户的主页.我们就来实现相似的效果.直接上代码吧. @Over ...

  9. net开发过程中Bin目录net开发过程中Bin目录下面几种文件

    .net开发过程中Bin目录下面几种文件格式的解释 在.NET开发中,我们经常会在bin目录下面看到这些类型的文件: .pdb..xsd..vshost.exe..exe..exe.config..v ...

  10. Vim常见操作汇总

    1.跳到文本的最后一行:按“G”,即“shift+g”2.跳到最后一行的最后一个字符 : 先重复1的操作即按“G”,之后按“$”键,即“shift+4”.3.跳到第一行的第一个字符:先按两次“g”,4 ...