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         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
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)。求一个大的值,我们会多次求解多个小的值,如果能把这些小值的解保存下来,就会节省很多时间。代码如下:

 public class Solution {
public HashMap<Integer, Integer> map = new HashMap();
public int numTrees(int n) {
map.put(0, 1);
map.put(1, 1);
if(map.containsKey(n)) return map.get(n); int count = 0;
for(int i = 1; i<=n; i++){
int left = map.containsKey(i-1)?map.get(i-1):numTrees(i-1);
int right = map.containsKey(n-1)?map.get(n-i):numTrees(n-i);
count += left*right;
}
map.put(n,count);
return count;
}
}

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. php生成图片缩略图的类方法

    //php生成缩略图片的类 class ResizeImage{ public $type;//图片类型 public $width;//实际宽度 public $height;//实际高度 publ ...

  2. 2016-12-14 - SSH Tunnel

    2016-12-14 - SSH Tunnel ssh Network Topo Network Topo A: Internet sshd B: NAT sshd C: NAT B Create S ...

  3. Activity的生命周期和启动模式

    Activity的生命周期分析 典型情况下的生命周期.是指在用户参与的情况下,Activity所经过的生命周期的改变. 异常情况下的生命周期.是指Activity被系统回收或者由于当前设备的Confi ...

  4. 第四章 android 命名规范和编码规范

    书里面讲的比较常见,单个人也是有不同的观点: 因为android绝大部分使用java开发的,因此java相关规范适用于android: Google Style: 英文地址:http://google ...

  5. Ray Tracing

    Ray Tracing 题目链接:http://codeforces.com/problemset/problem/724/C 拓展欧几里得 //为什么这次C题这么难啊=.= 可以观察到,光线在矩形中 ...

  6. express学习点滴- 永远不要忘记异步

    直接上两段代码,因为nodejs基于异步和事件回调的解决方式,涉及到异步的时候,问题往往藏得很深,以下这个简单的问题困扰了很久.之前怀疑是各种问题,到处改.直到最后一步一步跟代码,跟操作数据库部分豁然 ...

  7. html5权威指南:用元数据元素说明文档

    用元数据元素说明文档: html meta标签使用总结:http://www.cnblogs.com/lovesong/p/5745893.html html中meta标签使用介绍:http://ww ...

  8. HTML <base> 标签的 href 属性

    为页面上所有相对 URL 规定基准 URL: <head> <base href="http://www.w3school.com.cn/i/" /> &l ...

  9. 玩玩RMI

    今天在看代理设计模式,java中远程代理的实现一定会用到RMI的,很久没有温习过RMI的知识了,今天就重新过一遍这个知识点来让自己加深印象,构建一个简单的RMI小程序需要用到一下几个类: java.r ...

  10. ActiveMQ in Action(1) - JMS

    关键字: activemq 1 JMS    在介绍ActiveMQ之前,首先简要介绍一下JMS规范.1.1 JMS的基本构件1.1.1 连接工厂    连接工厂是客户用来创建连接的对象,例如Acti ...