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. linux下之mysql篇

    网上查到的一般是 yum install mysql yum install mysql-server yum intall mysql-devel 但是在centos7下  mysql-server ...

  2. pe如何安装ios系统

    1.进PE系统(老毛桃) 2.虚拟光驱加载ios系统 3.然后打开我的电脑,里面有个光盘,就像光盘插在光驱里打开电脑后的样子,双击安装系统.

  3. SQL Server2008 R2开启远程连接总结

    ============================== SQL Server2008 R2开启远程连接(最全总结) ============================== 安装过程:适用W ...

  4. python print 控制台输出中文

    在pycharm里面的控制台用print输出信息,  本意想输出中文, 但是实际上是u\xxxx. 可以用这种方式: print("%s " % cn_string)

  5. Scriptcase v8推出内部測试版及价格调整

    PHP代码生成器Scriptcase如今已经内部推出了v8.0版本号,8.0相比于曾经的版本号做了大幅度调整.我们将在完好測试的基础上发表8.0的相关文章. 另,即日起,Scriptcase的20%优 ...

  6. Mysql root 用户密码忘记后重置root密码

    [windows] 1.停止mysql服务:打开命令行窗口CMD,Net stop mysql 2.用另外一种方式启动Mysql:在命令行进入到mysql的安装路径下的bin目录下使用 mysqld- ...

  7. ruby里面的属性访问器

    和ios的@property一样 attr_accessor 表明是示例的getter和setter 下面的是rails的扩展,裸体class里面用,貌似会报错 cattr_accessor 表明是类 ...

  8. 基于C#的微信公众平台开发系列1

    1.首先服务器地址及Token验证: Token验证请求地址wx.ashx代码: using System; using System.Web; public class wx : IHttpHand ...

  9. Bootstrap-CL:分页

    ylbtech-Bootstrap-CL:分页 1.返回顶部 1. Bootstrap 分页 本章将讲解 Bootstrap 支持的分页特性.分页(Pagination),是一种无序列表,Bootst ...

  10. SSH&SFTP服务分离+家目录锁定

    Step 1 在root用户下创建维护账号的家目录,此处以创建userftp帐号的家目录为例. mkdir -p /chroot/home/user Step 2 在root用户根目录下执行以下命令设 ...