96. Unique Binary Search Trees

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

解析

  • LeetCode96:Unique Binary Search Trees
  • 给定一个序列1…..n,为了构造所有二叉树,我们可以使用1……n中的每一个数i作为根节点,自然1……(i-1)必然位于树的左子树中,(i+1)…..n位于树的右子树中。然后可以递归来构建左右子树,由于根节点是唯一的,所以可以保证构建的二叉树都是唯一的。

使用两个状态来记录:

G(n):长度为n的序列的所有唯一的二叉树。

F(i,n),1<=i<=n:以i作为根节点的二叉树的数量。

G(n)就是我们要求解的答案,G(n)可以由F(i,n)计算而来。

G(n)=F(1,n)+F(2,n)+…+F(n,n) (1)

G(0)=1,G(1)=1

对于给定的一个序列1…..n,我们取i作为它的根节点,那么以i作为根节点的二叉树的数量F(i)可以由下面的公式计算而来:

F(i,n)=G(i-1)*G(n-i) 1<=i<=n (2)

综合公式(1)和公式(2),可以看出:

G(n) = G(0) * G(n-1) + G(1) * G(n-2) + … + G(n-1) * G(0)

《96. Unique Binary Search Trees》

// 96. Unique Binary Search Trees
class Solution_96 {
public:
    int numTrees(int n) {
        //卡特兰数
        long long ans = 1;
        for (int i = n + 1; i <= 2 * n;i++)
        {
            ans = ans*i / (i-n);
        }
        return ans / (n + 1);
    }
};

题目来源

    原文作者:ranjiewen
    原文地址: https://www.cnblogs.com/ranjiewen/p/8834551.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞