[动态规划] leetcode 96 Unique Binary Search Trees

problem:https://leetcode.com/problems/unique-binary-search-trees/

左子树个数 * 右子树个数

class Solution {
public:
    int numTrees(int n) {
        vector<int> dp(n + 1);
        dp[1] = 1;
        for(int i = 2;i <= n;i++)
        {
            dp[i] = dp[i - 1] + dp[i - 1];
            for(int j = 1;j <= i - 1; j++)
            {
                dp[i] += dp[j] * dp[i - 1 - j];
            }
        }
        return dp[n];
    }
};

原文地址:https://www.cnblogs.com/fish1996/p/11329808.html

时间: 2024-10-08 03:54:16

[动态规划] leetcode 96 Unique Binary Search Trees的相关文章

leetCode 96.Unique Binary Search Trees (唯一二叉搜索树) 解题思路和方法

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 "{1,#,2,3}

LeetCode 96 Unique Binary Search Trees不同的二叉搜索树的个数

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 1 class Solution { 2 public: 3 int numTrees(int n) { 4 int *dp = new int[n+1]; 5 for(int i=0;i<n+1;++i) 6 dp[i] = 0; 7 8 dp[0] = 1;//!!! 9 for(int sz = 1; sz <

leetcode 96 Unique Binary Search Trees ----- 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 给一个正整数n,然后将这n个数进行二叉排序树的排列,求有多少种组合. public class Sol

Java [Leetcode 96]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 解题思路: 动态规划法. 用G(n)表示长度为n组成的二叉搜索树的数目: G(0) = 1

Leetcode #96 Unique Binary Search Trees

题目链接:https://leetcode.com/problems/unique-binary-search-trees/ 从最简单的情况开始考虑: n = 0 : 只有“空树”这一种情况.f[0] = 1. n = 1 : 只有“根节点”这一种情况.f[1] = 1. n = 2 : 由于“根节点”必须存在,所以只有一个节点的位置是可以变化的.即“左子树0个节点,右子树1个节点”,或者“左子树1个节点,右子树0个节点”.f[2] = f[0] * f[1] + f[1] * f[0]. 同理

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

leetcode -day28 Unique Binary Search Trees I II

1.  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 /

96. Unique Binary Search Trees &amp;&amp; 95. Unique Binary Search Trees II &amp;&amp; 241. Different Ways to Add Parentheses

96. 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 Tree Dynamic Program

Java for LeetCode 095 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. 解题思路: 参考Java for LeetCode 096 Unique Binary Search Trees思路,本题很容易解决.注意,