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

终于开始动态规划的题目了。这道题目需要耐心挖掘其中的规律,首先根不一样的二叉搜索树肯定是不同的搜索树。依次类推,如果根结点一样,但是根结点下的一层结点不一样,则也是不一样的二叉搜索树。只要叶子结点的上一层结点有结点不一样,则是不一样的二叉搜索树。依次类推。从n为0开始列举:

n = 0

n = 1

1

n = 2

1                  2

\                /

2            1

n = 3

1           3    3      2     1

\        /     /       / \       \

3    2    1      1   3      2

/     /        \                    \

2   1          2                   3

定义f(n)为unique BST的数量,以n = 3为例:

构造的BST的根节点可以取{1, 2, 3}中的任一数字。

如以1为节点,则left subtree只能有0个节点,而right subtree有2, 3两个节点。所以left/right subtree一共的combination数量为:f(0) * f(2) = 2

以2为节点,则left subtree只能为1一个节点,right subtree只能为2个节点:f(1) * f(1) = 1

以3为节点,则left subtree有1, 2两个节点,right subtree有0个节点:f(2)*f(0) = 2

所以总结以上可以得出规律,f(n)=f(0)*(n-1)+f(1)*f(n-2)+f(2)*f(n-3)+.....f(n-1)*f(0)为卡特兰数。

class Solution(object):
    def numTrees(self, n):
        """
        :type n: int
        :rtype: int
        """
        f = [0]*(n+1)
        f[0] = 1
        for i in xrange(1,n+1):
            for j in xrange(n):
                f [i] += f[j]*f[i-1-j]
        return f[n]

DP解法,自底向上,总体循环次数为1+2+...n,复杂度为O(n^2),空间复杂度为O(n),为存储中间结果的数组大小。

时间: 2024-10-10 17:34:49

Unique Binary Search Trees的相关文章

Unique Binary Search Trees I & II

Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? 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 分析: 当数组为 1,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:以i为根节点的树,其

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

1. 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 题目分析参考自一博

[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

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_96题——Unique Binary Search Trees(动态规划)

Unique Binary Search Trees Total Accepted: 48675 Total Submissions: 134436My Submissions Question Solution 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 uniq

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 /

[leetcode]Unique Binary Search Trees II @ Python

原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上一题要求的是符合条件的二叉查找树的棵数,我们上一题提过,求个数一般思路是动态规划,而枚举的话,我们就考虑dfs了.dfs(start, end)函数返回以start,start+1,...,end为根的二叉查找树. 代码: # Definition for a binary tree node #

经典中的经典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 / /

Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树

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 算法分析:类似上阶梯,