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. IAsyncResult 接口异步 和匿名委托

    IAsyncResult 接口异步 DataSet ds = new DataSet(); Mydelegate del = new Mydelegate(LoadData); IAsyncResul ...

  2. MD5加密 js文件

    var hexcase = 0; var b64pad = ""; var chrsz = 8; function hex_md5(s){ return binl2hex(core ...

  3. SQL 范式(转载)

    装载于"http://www.cnblogs.com/KissKnife/ 理论性的东西,往往容易把人人都看得懂的东西写成连鬼都看不懂,近似于主任医生开的药方.从前学范式的时候,把书中得概念 ...

  4. python websocket学习使用

    前言 今天看了一些资料,记录一下心得. websocket是html5引入的一个新特性,传统的web应用是通过http协议来提供支持,如果要实时同步传输数据,需要轮询,效率低下 websocket是类 ...

  5. Chapter 2 Open Book——29

    Which left me with nothing to do but try to not look at him…unsuccessfully. 我没什么事情可以做但是我尝试不去看他,但是失败了 ...

  6. Git回退到服务器某个版本正确姿势

    背景: Git协作中,成员不可避免地会提交一些错误的版本,由于Git相比SVN引入了本地仓库,操作会相对复杂,以下为姿势分解 找一个源文件RspUtils.java,加上一行注释 //测试回退git服 ...

  7. kali自动化清理缓存和日志

    前几天发现kali无法正常开机,启动盘启动进去之后, 发现/var/log/目录下的日志文件太大把硬盘占满了,于是乎... 文件主要是这三个: kern.log :   包含内核产生的日志,有助于在定 ...

  8. 《JS权威指南学习总结--8.8.2高阶函数》

    内容要点: 所谓高阶函数(higher-order function)就是操作函数的函数,它接收一个或多个函数作为参数,并返回一个新函数. 例1: //这个高阶函数返回一个新的函数,这个新函数将它的实 ...

  9. Qt4.8.x Linux WebKit依赖库安装

    yum install "pkgconfig(gstreamer-app-0.10)"

  10. visible绑定(The "visible" binding)

    对visible进行绑定可以控制元素的显示和隐藏. 示例: <div data-bind="visible: shouldShowMessage"> You will ...