leetcode[94] Unique Binary Search Trees

给定n,那么从1,2,3...n总共可以构成多少种二叉查找数呢。例如给定3

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

思路:

我们考虑头结点i,那么所有比i小的都在i的左边,比i大的都在i的右边。也就是以i为开头的是i的左边的可能*i右边的可能,然后遍历i从1到n,所有可能相加就是我们的结果。

由公式 h[n] = h[0]*h[n-1] + h[1]*h[n-1] + ... + h[n-1]*h[0]; 可得如下:

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

其实这是一个卡特兰数,直接用公式C2n选n除以n+1则如下:

class Solution {
public:

    int numTrees(int n) {
        if (n == 1) return 1;
        long long denominator = 1, numerator = 1;
        int cnt = 2 * n;
        while(cnt > n) denominator *= cnt--;
        while(cnt > 0) numerator *= cnt--;
        return denominator/numerator/(n+1);
    }
};

还可以用递归

class Solution {
public:
    int numTrees(int n)
    {
        return numTrees(1,n);
    }

    int numTrees(int start, int end)
    {
        if (start >= end)
            return 1;

        int totalNum = 0;
        for (int i=start; i<=end; ++i)
            totalNum += numTrees(start,i-1)*numTrees(i+1,end);
        return totalNum;
    }
};
时间: 2024-11-22 23:20:57

leetcode[94] 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 /

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][JavaScript]Unique Binary Search Trees II

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

【LeetCode】Unique Binary Search Trees II 不同的二叉查找树II

今早起来做 LeetCode,结果被这道题卡了将近1个半小时,忍着没有去搜答案,最后还是被我想出来了,而且直接一次AC,哈哈!现在在这里记录一下解题思路. 原题: 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 sh

【LeetCode】Unique Binary Search Trees (2 solutions)

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 Base case: n==0, n==1时,f

[C++]LeetCode: 92 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 confused what "{1,#,

[LeetCode][Java]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 题意: 给定n ,生成所有的存在且唯一的

【Leetcode】Unique Binary Search Trees II

题目链接:https://leetcode.com/problems/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.