[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卡塔兰数的一个例子,如果对卡塔兰数不熟悉的童鞋可能真不太好做。话说其实我也是今天才知道的好嘛-.-|||,为啥我以前都不知道捏?!为啥卡塔兰数不像斐波那契数那样人尽皆知呢,是我太孤陋寡闻么?!不过今天知道也不晚,不断的学习新的东西,这才是刷题的意义所在嘛! 好了,废话不多说了,赶紧回到题目上来吧。我们先来看当 n = 1的情况,只能形成唯一的一棵二叉搜索树,n分别为1,2,3的情况如下所示:

                    1                        n = 1

                2        1                   n = 2
               /                        1            2

   1         3     3      2      1           n = 3
    \       /     /      / \           3     2     1      1   3      2
    /     /       \                    2     1         2                 3

就跟斐波那契数列一样,我们把n = 0 时赋为1,因为空树也算一种二叉搜索树,那么n = 1时的情况可以看做是其左子树个数乘以右子树的个数,左右字数都是空树,所以1乘1还是1。那么n = 2时,由于1和2都可以为跟,分别算出来,再把它们加起来即可。n = 2的情况可由下面式子算出:

dp[2] =  dp[0] * dp[1]   (1为根的情况)

    + dp[1] * dp[0]    (2为根的情况)

同理可写出 n = 3 的计算方法:

dp[3] =  dp[0] * dp[2]   (1为根的情况)

    + dp[1] * dp[1]    (2为根的情况)

     + dp[2] * dp[0]    (3为根的情况)

由此可以得出卡塔兰数列的递推式为:

我们根据以上的分析,可以写出代码如下:

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

 

时间: 2024-08-28 14:37:14

[LeetCode] 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] Validate Binary Search Tree 验证二叉搜索树

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

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

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#,2,3}"

[LeetCode] 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,#,2,3}&

LeetCode: Unique Binary Search Trees [095]

[题目] 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, 问用1,2,3,4,5...n这n个值,能构造多少棵合法的二叉

LeetCode: Unique Binary Search Trees II [096]

[题目] 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——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 原题链接:https://oj.leetcode.com/problems/unique-binar

[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 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 class Solution { private