一、     称号

给定的数目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. 客户端上显示csdn上的各类别下的的文章列表 (制作csdn app 三)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/23597229 今天将在Android 使用Fragment,ViewPagerI ...

  2. Chrome 小工具: 启动本地应用 (Native messaging)

    最近遇到一个新的问题.需要使用Chrome 插件, 从我们对我们当地的一个网站之一启动C#应用,同时通过本申请值执行不同的操作. 在这里记录下解决的过程.以便以后查找 首先我们须要新建一个google ...

  3. php学习笔记--高级教程--读取文件、创建文件、写入文件

    打开文件:fopen:fopen(filename,mode);//fopen("test.txt","r"): 打开模式:r  仅仅读方式打开,将文件指针指向 ...

  4. c++头

    头文件c/c++独特的概念. 首先解释声明和定义的区别. extern int x;这是一个可变x声明,void fun();这是函数fun()声明.class a;这是类a声明. int x;变量x ...

  5. ASP.NET自定义控件组件开发 第三章 为控件添加事件 后篇

    原文:ASP.NET自定义控件组件开发 第三章 为控件添加事件 后篇 第三章 为控件添加事件 后篇 前一篇文章只是简单的说了下事件,但是大家应该方法,在ASP.NET自定义控件中只是简单那么定义事件是 ...

  6. sharepoint 2013 个人网站公共母板页路径地址

    C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\FEATURES\MySiteUnif ...

  7. C语言标准库函数qsort具体解释

    1 函数简单介绍 功 能: 使用高速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(c ...

  8. opengl微发展理解

    1.什么是OpenGL? 一种程序,可以与界面和图形硬件交互作用.一个开放的标准 2.软件管道 请看上图 - Apllication层     表示你的程序(调用渲染命令.如opengl API) - ...

  9. JSON 数据使用

    当用不同的数据的模板需要更换时.假设数据点的量.使用json非常方便. json物: var JSONObject= { "name":"Bill Gates" ...

  10. 网络视频播放器插件ckplayer使用-简介

    ckplayer插件下载:http://pan.baidu.com/s/12HYH4(假设不见了,下载您自己的地址,下载后添加到站点根文件夹) ******特别提醒:解压后不要忘了把js文件夹也加入到 ...