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 Solution {
    public int numTrees(int n){
        if( n < 3)
            return n;
        int[] dp = new int[n+1];
        dp[0] = 1;
        dp[1] = 1;
        dp[2] = 2;
        for( int i = 3;i<n+1;i++){
            for( int j = 0;j<i;j++){
                dp[i] += dp[j]*dp[i-j-1];
            }
        }
        return dp[n];
    }
}
时间: 2024-07-30 19:46:34

leetcode 96 Unique Binary Search Trees ----- java的相关文章

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不同的二叉搜索树的个数

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 (唯一二叉搜索树) 解题思路和方法

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

题目链接: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 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 -

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思路,本题很容易解决.注意,

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