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 3 3 2 1
  2. \ / / / \ \
  3. 3 2 1 1 3 2
  4. / / \ \
  5. 2 1 2 3
 
Solution 1:  递归
  1. class Solution {
  2. public:
  3. int numTrees(int n) {
  4. if(n == )
  5. return ;
  6. else if(n == )
  7. return ;
  8. else
  9. {
  10. int count = ;
  11. for(int i = ; i <= (n-)/; i ++)
  12. {
  13. if(i < n--i)
  14. count += *numTrees(i)*numTrees(n--i);
  15. else
  16. count += numTrees(i)*numTrees(n--i);
  17. }
  18. return count;
  19. }
  20. }
  21. };

Solution 2: dynamic programming . Base case: n==0, n==1时,f(n)==1, f(n)=∑f(i)*f(n-i-1)。即以第i个为根节点,左右子树数目相乘

  1. class Solution {
  2. public:
  3. int numTrees(int n) {
  4. if(n<)return ;
  5.  
  6. vector<int> v(n+, );
  7. v[]=;
  8. v[]=;
  9. for(int i=;i<n+;i++){
  10. for(int j=;j<i;j++){
  11. v[i]+=v[j]*v[i--j];
  12. }
  13. }
  14. return v[n];
  15. }
  16. };
  1.  

【LeetCode】96 - Unique Binary Search Trees的更多相关文章

  1. 【LeetCode】96. Unique Binary Search Trees (2 solutions)

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

  2. 【LeetCode】96. Unique Binary Search Trees 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化递归 动态规划 卡特兰数 日期 题目地址:ht ...

  3. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  4. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

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

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

  6. 【一天一道LeetCode】#95. Unique Binary Search Trees II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. LeetCode OJ 96. Unique Binary Search Trees

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

  8. [leetcode tree]96. Unique Binary Search Trees

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

  9. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

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

随机推荐

  1. jQuery-瀑布流-绝对定位布局(二)(延迟AJAX加载图片)

    jQuery-瀑布流-绝对定位布局(二)(延迟AJAX加载图片)   瀑布流-绝对定位布局,与浮动布局的区别在于 1.布局不一样: 绝对定位:一个UL里面放置所有的绝对定位的LI: 浮动布局:多个(一 ...

  2. UX结合需求实例化进行设计开发

    技  术  文  件 技术文件名称:实例化+UX需求分析实践:场景监控需求实例化 技术文件编号: 版        本:V1.0 共 32 页 (包括封面) 拟  制    廖开蒙.刀锋团队 审  核 ...

  3. SQL —— 一些需要注意的地方(持续更新)

    TRUNCATE 只适用全表,没有 WHERE 语句 rownum < N 不能和 group by 一起使用 NULL 值通常会限制索引.在创建表时对某一列指定 NOT NULL 或 DEFA ...

  4. [原]poj2243-Knight Moves-水bfs

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...

  5. 安装 jdk、tomcat

    jdk 下载地址:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java ...

  6. Introducing Microsoft Sync Framework: Sync Services for File Systems

    https://msdn.microsoft.com/en-us/sync/bb887623 Introduction to Microsoft Sync Framework File Synchro ...

  7. 利用XPath读取Xml文件

    之所以要引入XPath的概念,目的就是为了在匹配XML文档结构树时能够准确地找到某一个节点元素.可以把XPath比作文件管理路径:通过文件管理路 径,可以按照一定的规则查找到所需要的文件:同样,依据X ...

  8. 彻底搞清javascript中this, constructor, prototype

    说起这三个属性,肯定有一些同学和我一样,初学js时非常困惑,头大,一脸的迷茫.今天就来给大家彻底解决这些担心受怕的问题. 先看this this定义: this就是函数赖以执行的对象. 分析这句话: ...

  9. PHPStorm/webstorm tips

    phpstorm对于使用PHP开发web的人员来说,是一个非常不错的编辑开发IDE,以前用过sublime,但是相比于storm,sublime在浏览legacy代码,类代码编辑方面明显要逊色不少.同 ...

  10. HDU 3068 (Manacher) 最长回文

    求一个字符串的最长子串,Manacher算法是一种O(n)的算法,很给力! s2[0] = '$',是避免在循环中对数组越界的检查. 老大的代码: http://www.cnblogs.com/Big ...