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
 
 法I:后序遍历。同Unique Binary Search Trees II,只是现在要返回的是数量,而无需将子树的所有情况放在数组中返回。
class Solution {
public:
int numTrees(int n) {
if(n==) return ;
else return postOrderTraverse(,n);
} int postOrderTraverse(int start, int end)
{
if(start == end){ //递归结束条件:碰到叶子节点(没有子节点)
return ;
} int leftNum, rightNum, num=;
for(int i = start; i<=end; i++){ //iterate all roots
leftNum = ;
rightNum = ;
if(i > start){ //count left tree
leftNum = postOrderTraverse(start, i-);
}
if(i < end){ //count right tree
rightNum = postOrderTraverse(i+, end);
} //visit root: count number for each (leftTree, rightTree) pair
if(leftNum==) num+=rightNum;
else if(rightNum==) num+=leftNum;
else num+=leftNum*rightNum;
}
return num;
}
};

Result:  Time Limit Exceeded

法II:法I超时的原因是进行了很多重复计算。比如,在1-3的子树数量和4-6子树数量是相通的,因为它们的数字跨度相同。

解决方法是用动态规划存储每种数字跨度下的子数数量

class Solution {
public:
int numTrees(int n) {
if(n==) return ;
int dp[n+];
dp[] = ;
dp[] = ;
for(int i = ; i <= n; i++){
dp[i] = ; //initialize
for(int j = ; j <= i; j++){ //traverse root
if(i-j> && j>) //左、右子树都存在
dp[i] += dp[j-]*dp[i-j];
else if(j>) //only left tree
dp[i] += dp[j-];
else //only right tree
dp[i] += dp[i-j];
}
}
return dp[n];
} };

96. Unique Binary Search Trees (Tree; DP)的更多相关文章

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

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

  2. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

  3. 52. leetcode 96. Unique Binary Search Trees

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

  4. 96. Unique Binary Search Trees

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

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

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

  6. 96. Unique Binary Search Trees(I 和 II)

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

  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】#96. Unique Binary Search Trees

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

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

随机推荐

  1. .NET移动开发环境搭建

    开发工具:Xamarin Studio 社区版 下载地址 http://www.monodevelop.com/download/ 操作系统要求:Windows7及以上..NET Framework4 ...

  2. DesignPattern(二) 创建型模式

    创建型模式 创建型模式就是用来创建对象的模式,抽象了实例化的过程.所有的创建型模式都有两个共同点.第一,它们都将系统使用哪些具体类的信息封装起来:第二,它们隐藏了这些类的实例是如何被创建和组织的.创建 ...

  3. 【java多线程】ConCurrent并发包 - Lock详解

    synchronized的缺陷   我们知道,可以利用synchronized关键字来实现共享资源的互斥访问. Java 5在java.util.concurrent.locks包下提供了另一种来实现 ...

  4. memsql filesystem pipeline 试用

    一些功能类似drill ,比如s3,file ... 创建file pipeline 准备file mkdir -p /opt/db/ touch books.txt 内容如下: The Catche ...

  5. Javascript模块化编程require.js的用法

    JS模块化工具requirejs教程(一):初识requirejs http://www.runoob.com/w3cnote/requirejs-tutorial-1.html JS模块化工具req ...

  6. 写时复制和fork,vfork,clone

    写时复制 原理: 用了“引用计数”,会有一个变量用于保存引用的数量.当第一个类构造时,string的构造函数会根据传入的参数从堆上分配内存,当有其它类需要这块内存时,这个计数为自动累加,当有类析构时, ...

  7. python 面向对象 初始化

    参考学习: http://www.runoob.com/python/python-object.html 其中 函数里面 self.name 就是用 初始化的 name Employe.empCou ...

  8. Bootstrap-Plugin:滚动监听(Scrollspy)插件

    ylbtech-Bootstrap-Plugin:滚动监听(Scrollspy)插件 1.返回顶部 1. Bootstrap 滚动监听(Scrollspy)插件 滚动监听(Scrollspy)插件,即 ...

  9. 深入理解 Express.js

    本文针对那些对Node.js有一定了解的读者.假设你已经知道如何运行Node代码,使用npm安装依赖模块.但我保证,你并不需要是这方面的专家.本文针对的是Express 3.2.5版本,以介绍相关概念 ...

  10. Hibernate中get()和load()的区别

    Hibernate中根据Id单条查询获取对象的方式有两种,分别是get()和load(),来看一下这两种方式的区别. 1. get() 使用get()来根据ID进行单条查询: User user=se ...