leetcode:96. Unique Binary Search Trees

来源:互联网 发布:先导爱知 刻关系 编辑:程序博客网 时间:2024/06/06 02:11

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来写

0 0
原创粉丝点击