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}" means? >
read more on how binary tree is serialized on OJ.

思路:此题与上一题相比,不需要具体的树结构,但是要返回数量,如果用上一题的思路来做,肯定是会超时。

所以,个人感觉,本题要比上题要难,难在方法的不好理解。‘参考网上资料之后,其总结的动态规划的公式也很难自己想到。

私以为,这题通过率很高,大部分都是因为个人没有思路,在网上参考之后解出的。

具体代码如下:

public class Solution {
    public int numTrees(int n) {
        /**
         * 另一种解法则是找出递推公式
         * f(n) = 2(2n-1)f(n-1)/(n+1)
         * 参考资料:http://blog.sina.com.cn/s/blog_71d59f9a01017irg.html
         */

         if(n <= 1)
            return 1;

         long f2 = 0,f1 = 1;//long防止溢出
         for(int i = 2; i <= n; i++){
             f2 = 2*(2*i-1)*f1/(i+1);
             f1=f2;
         }
         return (int)f2;

    	/**
    	 * 本题动态规划的思想
    	 * 但是状态转移方程很难,不参考资料做出来是比较厉害的
    	 */
/*        if(n <= 1)
            return 1;

        int[] f = new int[n+1];
        f[1] = f[0] = 1;

        for(int i = 2; i <= n ; i++){
            for(int j = 0; j < i; j++){
                f[i] += f[j]*f[i-j-1];
            }
        }
        return f[n];*/
    }
}

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

时间: 2024-10-14 00:38:37

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

[LeetCode] 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 这道题实际上是Catalan Number卡塔兰数的一个例子,如果对卡塔兰数不熟悉的童鞋可能真不太好做

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 <

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

[LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2]   1   /  3     2 Output: [3,1,null,null,2]   3   /  1     2 Example 2: Input: [3,1,4,null,null,2]

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 -

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the sequence is unique. Follow up:Could you do it using only constant space complexity? 给一个数组,验证是否为一个二叉搜索树的

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