一、     称号

给定的数目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. POJ3623:Best Cow Line, Gold(后缀数组)

    Description FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year&qu ...

  2. 一个简单的样例看明确怎样利用window.location.hash实现ajax操作时浏览器的前进/后退功能

    我们知道JavaScript中非常早就提供了window.history对象,利用history对象的forward().go().back()方法可以方便实现不同页面之间的前进.后退等这样的导航功能 ...

  3. Unity3D-RPG项目实战(1):发动机的特殊文件夹

    前言 从8月份開始.下定决心正式開始学习Unit3D啦.尽管自己写过两代端游引擎,被应用的项目也超过10个,Unreal Engine也搞过几年,只是做手游.哥确实还是个新手.Unity3D这个引擎我 ...

  4. 简介Customer Care Accelerator (CCA)

    几个月前,我们发表了CRM4.0的附属插件:Customer Care Accelerator (CCA). 年以来CCA已经存在,我们这些新手在CRM的世界里, Customer Care Fram ...

  5. hdu 4691 最长的共同前缀 后缀数组 +lcp+rmq

    http://acm.hdu.edu.cn/showproblem.php? pid=4691 去年夏天,更多的学校的种族称号.当时,没有后缀数组 今天将是,事实上,自己的后缀阵列组合rmq或到,但是 ...

  6. Reorder List [leetcode] 这两种思路

    第一个想法随着vector保存全部Node* 表拼接出来 void reorderList(ListNode *head) { vector<ListNode*> content; Lis ...

  7. Hibernate在关于一对多,多对一双向关联映射

    [Hibernate]之关于一对多,多对一双向关联映射 因为一对多.和多对一的双向关联映射基本上一样,所以这里就一起写下来! Annotations配置 @Entity @Table(name=&qu ...

  8. ASP.NET MVC 5– 采用Wijmo MVC 5模板1创建应用程序分钟

    启用 采用ComponentOne Studio for ASP.NET Wijmo制作MVC5应用,首先要做的就是安装pid=4&from=MVC4DOC">Studio f ...

  9. 跨容器Hybrid离线组件方案

    关键词:跨容器.Hybrid.离线 摘要:今天主要讨论的是离线组件跨容器方案,想了解在线页面如何跨webview容器,可以看 http://www.cnblogs.com/yexiaochai/p/5 ...

  10. sqlserver缓存程序-只能使用一次清除缓存计划

    plan cache非常大.将仅仅使用一次的缓存计划清除,而不用清除整个cache. declare @sid varbinary(64) declare cur01 cursor for selec ...