leetcode:96. Unique Binary Search Trees

原创 2016年06月01日 10:09:50

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

/**
 * @param {number} n
 * @return {number}
 */
var numTrees = function(n) {
    var map = [1,1];
    for(var i = 2; i <= n; i++) {
        map[i] = 0;
        for(var j = 1; j <= i; j++) {
                map[i] += (map[j-1] * map[i-j])
            }
    }
    return map[n];
};
动态规划的题,尝试了下用js来写

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode-96.Unique Binary Search Trees

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

LeetCode 96. Unique Binary Search Trees 解题报告

LeetCode 108. Convert Sorted Array to Binary Search Tree 解题报告

Leetcode 96 Unique Binary Search Trees(独一的二叉搜索树)

一,问题描述 1,给定一个n,返回所有的二叉搜索树的总数。2,例如: 输入:n=4 输出:14输入:n=3 输出:5输入:n=5 输出:42输入:n=6; 输出:132输入: n=2 输...

<LeetCode OJ> 96. Unique Binary Search Trees

96. Unique Binary Search Trees My Submissions Question Total Accepted: 75913 Total Submissions: ...

[leetcode]96. Unique Binary Search Trees(Java)

https://leetcode.com/problems/unique-binary-search-trees/#/description Given n, how many ...

leetcode_middle_45_96. Unique Binary Search Trees

题意: 给一个n,问能存储从1到n 的二叉排序树有多少个。 分析: 我们对前几个比较小的树,举例子画图分析。我们一定要搞清楚,为什么3个结点就是5个树,4个结点就是14个树。多画了几个树之后,很明显...
  • pusude
  • pusude
  • 2017年02月15日 14:08
  • 97

[leetcode] 96.Unique Binary Search Trees

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

LeetCode 95, 96. Unique Binary Search Trees i. ii

1. 题目描述 95.Given n, generate all structurally unique BST’s (binary search trees) that store values...

LeetCode --- 96. Unique Binary Search Trees

题目链接:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) t...
  • makuiyu
  • makuiyu
  • 2015年04月14日 19:49
  • 636

leetcode 95&96. Unique Binary Search Trees

96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:96. Unique Binary Search Trees
举报原因:
原因补充:

(最多只允许输入30个字)