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. STL标准库-容器-list

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性. list 表示非连续的内存区域,并通过一对指向首尾元素的指针双向链接起来,从而允许向前和向后两个方向进行遍历.在list 的任 ...

  2. Java 工程师求职遇害|多一分警惕,少一份悲剧

    当朋友圈里满是战狼票房屡创新高的刷屏文章时,一则有关 Java 开发工程师李文星面试遇害的报道,却令人唏嘘不已.年仅23岁.正值青春年少.怀揣着通过打拼奋斗实现养家糊口梦想的大学毕业生,在初入职场的第 ...

  3. js base64 转成图片上传

    直接上代码,要点就是把base64转成Blob,添加到FormData传递给后台程序,跟选择图片文件上传时一样的了. var dataurl = canvas.toDataURL('image/png ...

  4. “之”字形输出二叉树 python

    “之”字形输出二叉树即第1层从左到右输出,第2层从右到左输出,第3层从左到右输出......第2*n层从右到左输出,第2*n+1层从左到右输出,如下图所示二叉树,“之”字形输出的结果为:1-3-2-4 ...

  5. SVN 定时 更新代码 Demo

    1. 涉及技术: Winservice: 用system身份后台跑:  Quartz:定时任务:  SVN 2. 思路: Quartz定时调用cmd 程序,执行SVN update 命令,整个程序寄宿 ...

  6. java第十次面试题

    一.给定一个字符串,把字符串内的字母转换成该字母的下一个字母,a换成b,z换成a,Z换成A,如aBf转换成bCg,字符串内的其他字符不改变,给定函数,编写函数. public static void ...

  7. Js 手风琴效果

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. 复利计算5.0(改成Java版本)与 单元测试

    //由于C语言版本不方便单元测试,所以改成了java版本,部分代码如下:import java.util.Scanner; public class FuLi{ public static void ...

  9. 每天进步一点点——Linux中的线程局部存储(一)

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/26469435    在Linux系统中使用C/C++进行多线程编程时,我们遇到最多的就是对同 ...

  10. JSON数组对象某个属性值查找

    1.引用国外开源Linq写法的js框架 地址:https://archive.codeplex.com/?p=jslinq https://www.nuget.org/packages/jslinq ...