Unique Binary Search Trees(dp)

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
BST树的定义:根节点左边所有节点的值小于根节点值,右边所有节点的值大于根节点的值,然后其左右子树又是BST

思路:例如序列:1,2,3,4,5

第一个数字的左边有0个数字,右边4个数字  dp[0]*dp[4]

第二个数字的左边有1个数字,右边3个数字  dp[1]*dp[3]

第三个数字的左边有2个数字,右边2个数字  dp[2]*dp[2]

第四个数字的左边有3个数字,右边1个数字  dp[3]*dp[1]

第五个数字的左边有4个数字,右边0个数字  dp[4]*dp[0]

所以我们要求的dp[5]就等于上面加起来的和,这里我们得假设dp[0]=1;

其实就是每个数字来当根节点,具体如下图所示


代码:
class Solution{
public:
    int numTrees(int n) {
        if(n<=1) return n;
        vector<int> dp(n+1,0);

        dp[0]=1;
        for(int i=1;i<=n;++i){
            int temp=0;
            for (int j=1;j<=i;++j)
            {
                temp+=dp[j-1]*dp[i-j];
            }
            dp[i]=temp;
        }
        return dp[n];
    }
};
时间: 2024-10-10 21:36:43

Unique Binary Search Trees(dp)的相关文章

leetcode_96题——Unique Binary Search Trees(动态规划)

Unique Binary Search Trees Total Accepted: 48675 Total Submissions: 134436My Submissions Question Solution 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 uniq

LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)

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 一开始是想用递归解决,但看了标签是dp问题,就想了一下, 数目为k的bst,其每个 0 ~ k - 1

[LeetCode] Unique Binary Search Trees II (难以忍受的递归)

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 class Solution { private

【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】

[096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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 \ / / /

LeetCode:: Unique Binary Search Trees[详细分析]

Unique Binary Search Trees My Submissions 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 设S(n)为n个

leetcode || 95、Unique Binary Search Trees II

problem: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 confused what "

Leetcode 树 Unique Binary Search Trees

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Binary Search Trees Total Accepted: 13478 Total Submissions: 37858 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there

Unique Binary Search Trees II leetcode java

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题解:这道题比1难的就是不是返回个数,而

Unique Binary Search Trees leetcode java

题目: 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 题解:  这道题我自己是没啥思路的,感觉就是一种排列组合的计算,但是又不太会..然后网上查了