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. VS编译wxWidgets

    准备工作 下载wxWidgets源码包(官网),我用的是3.02版: 安装Visual Studio.我用的是VS 2015 RC: 编译源码 解压wxWidgets的源码包,会得到一大堆文件.进入b ...

  2. Oracle SQL Developer 设置自动提示(完成设置)

  3. Linq入门

    一.Linq需要的C#语法支持: 1.隐式变量的使用var var使用时必须初始化      var是强类型数据 2.自动属性:public string FirstName{get ;set;} 3 ...

  4. prototype原型解析

    每个对象都有原型, prototype能够实现实例共享,节约使您有能力向对象添加属性和方法. 如 <script type="text/javascript"> fun ...

  5. openstack私有云布署实践【13.1 网络Neutron-compute节点配置(科兴环境)】

    所有kxcompute节点 下载安装组件   # yum install openstack-neutron openstack-neutron-linuxbridge ebtables ipset ...

  6. 淘淘商城_day06_课堂笔记

    今日大纲 实现单点登录系统 基于单点登录系统实现,用户的注册和登录 商品数据同步问题 问题 后台系统中将商品修改,前台系统没有进行数据的同步,导致前端系统不能够实时显示最新的数据. 解决 后台系统中商 ...

  7. HDU 2181 哈密顿绕行世界问题

    Problem Description 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前20行的第i行有3个数, ...

  8. Spring Security(11)——匿名认证

    目录 1.1     配置 1.2     AuthenticationTrustResolver 对于匿名访问的用户,Spring Security支持为其建立一个匿名的AnonymousAuthe ...

  9. js返回上一页并刷新代码整理

    一:JS 重载页面,本地刷新,返回上一页 复制代码 代码如下: <a href="javascript:history.go(-1)">返回上一页</a> ...

  10. C - 娜娜梦游仙境系列——吃不完的糖果

    C - 娜娜梦游仙境系列——吃不完的糖果 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Oth ...