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

我们观察一下BST的构成,对于任意一个节点,它的左子树上的所有值都比它小,它的右子树上的值都比它大。对于1···n,我们任选其中一个值i作为根节点,则小于i的有i-1个值,大于i的有n-1个值,因此对于以i为根节点这种情况,共计有numTrees(i-1)*numTrees(n-i)种可能。有了这种思路我们就可以递归求解。

但是这样的递归过程会重复做许多不必要的工作,例如n=5时,假设我们以3为根节点,会两次计算numTrees(2)。求一个大的值,我们会多次求解多个小的值,如果能把这些小值的解保存下来,就会节省很多时间。代码如下:

  1. public class Solution {
  2. public HashMap<Integer, Integer> map = new HashMap();
  3. public int numTrees(int n) {
  4. map.put(0, 1);
  5. map.put(1, 1);
  6. if(map.containsKey(n)) return map.get(n);
  7.  
  8. int count = 0;
  9. for(int i = 1; i<=n; i++){
  10. int left = map.containsKey(i-1)?map.get(i-1):numTrees(i-1);
  11. int right = map.containsKey(n-1)?map.get(n-i):numTrees(n-i);
  12. count += left*right;
  13. }
  14. map.put(n,count);
  15. return count;
  16. }
  17. }

LeetCode OJ 96. Unique Binary Search Trees的更多相关文章

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

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

  2. 【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 ...

  3. LeetCode OJ 95. 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】96. Unique Binary Search Trees 解题报告(Python & C++)

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

  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 tree]96. Unique Binary Search Trees

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

  7. LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

    题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...

  8. LeetCode OJ: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. C# 从零开始 vol.1

    说好的java只能先坑了,毕竟计划赶不上变化,以下是 c# 基础部分. 1:变量,方法的命名方式 目的就是一眼看到实例名 方法名 就知道该变量是做什么的. 主流的命名方式有驼峰命名规则,pascal命 ...

  2. java复习 --集合类

    List接口:可以存放重复内容: set接口:不能存放重复内容,重复内容依靠hashcode和equal两个方法来区分: Queue:队列: SortedSet接口:对集合中的数据进行排序:   Li ...

  3. 常用几种Java Web容器

    Web服务器是运行及发布Web应用的容器,只有将开发的Web项目放置到该容器中,才能使网络中的所有用户通过浏览器进行访问.开发Java Web应用所采用的服务器主要是与JSP/Servlet兼容的We ...

  4. 纯CSS实现斜角

    今天看了看腾讯的七周年时光轴,发现这个斜角的CSS,研究了半天提出下面代码可以直接实现斜角,不是CSS3哦,那个就太容易了 -webkit-transform:rotate(10deg); 倾斜度后再 ...

  5. USACO 1.3 Wormholes

    Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfire ...

  6. SVD分解技术数学解释

    SVD分解 SVD分解是LSA的数学基础,本文是我的LSA学习笔记的一部分,之所以单独拿出来,是因为SVD可以说是LSA的基础,要理解LSA必须了解SVD,因此将LSA笔记的SVD一节单独作为一篇文章 ...

  7. powder designer 转数据库

    1.打开“file new model”

  8. jQuery Post 提交内容中有标签报错

    Post编辑一点内容要传后台数据库: var html = editor2.html() console.log(encodeURIComponent(html)); //console.log(&q ...

  9. HDU 1407 测试你是否和LTC水平一样高(枚举)

    测试你是否和LTC水平一样高 Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上!你的任务是:计算方程x^2+y^2+z ...

  10. HDU 2614 Beat(DFS)

    题目链接 Problem Description Zty is a man that always full of enthusiasm. He wants to solve every kind o ...