给定n,那么从1,2,3...n总共可以构成多少种二叉查找数呢。例如给定3

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

思路:

我们考虑头结点i,那么所有比i小的都在i的左边,比i大的都在i的右边。也就是以i为开头的是i的左边的可能*i右边的可能,然后遍历i从1到n,所有可能相加就是我们的结果。

由公式 h[n] = h[0]*h[n-1] + h[1]*h[n-1] + ... + h[n-1]*h[0]; 可得如下:

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

其实这是一个卡特兰数,直接用公式C2n选n除以n+1则如下:

  1. class Solution {
  2. public:
  3.  
  4. int numTrees(int n) {
  5. if (n == ) return ;
  6. long long denominator = , numerator = ;
  7. int cnt = * n;
  8. while(cnt > n) denominator *= cnt--;
  9. while(cnt > ) numerator *= cnt--;
  10. return denominator/numerator/(n+);
  11. }
  12. };

还可以用递归

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

leetcode[94] Unique Binary Search Trees的更多相关文章

  1. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  2. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  3. Java for LeetCode 095 Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  4. [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

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

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

  6. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

  7. [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树

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

  8. [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  9. 【leetcode】Unique Binary Search Trees

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

随机推荐

  1. 如何解决卸载McAfee时出现“处于托管模式时无法删除”问题(转)

    问题现象: 这几天在为客户终端换装杀毒软件时出现这么一个问题:在控制面板的添加或删除程序里面将“McAfee VirusScan Enterprise和 McAfee AntiSpyware Ente ...

  2. HDOJ 4248 A Famous Stone Collector DP

    DP: dp[i][j]前i堆放j序列长度有多少行法, dp[i][j]=dp[i-1][j] (不用第i堆), dp[i][j]+=dp[i-1][j-k]*C[j][k] (用第i堆的k个石头) ...

  3. FastJson基本使用

    在发展中Android的过程中.假设我们常与server联系,更新数据等,然后,json它必须是一个良好的数据格公式,但随着json.使用原生的解析也能够,可是非常不高效,所以这里介绍两种json数据 ...

  4. 【LeetCode】Algorithms 题集(三)

    Search Insert Position 意: Given a sorted array and a target value, return the index if the target is ...

  5. NFS文件系统配置 和 GLIBC更新

    为了配置集群环境,把过程记录一下,方便后续使用 NFS 文件系统  是 network file system 配置好ssh无密码访问 ,各节点为centos6.5 主节点 在文件/etc/expor ...

  6. React组件开发入门

    React 组件开发入门 Introduction 本文组成: Ryan Clark文章Getting started with React的翻译. 博主的实践心得. React由Facebook的程 ...

  7. android_线

    说明:android螺纹. android无非就是一个线程Main Thread和Worker Thread.(除了主线程Main Thread是Worker Thread) Main Thread ...

  8. 使用Xcode和Instruments调试解决iOS内存泄漏

    尽管iOS 5.0加入版本号之后ARC机制,由于相互引用关系是复杂的.内存泄漏可能仍然存在.于是,懂原理是非常重要的. 这里讲述在没有ARC的情况下,怎样使用Instruments来查找程序中的内存泄 ...

  9. linux下面的中断处理软件中断tasklet机制

    參考: <Linux内核设计与实现> http://blog.csdn.net/fontlose/article/details/8279113 http://blog.chinaunix ...

  10. java.util.Timer demo good

    package timer; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import org ...