[Lintcode]163. Unique Binary Search Trees
发布时间:2020-12-14 04:25:17 所属栏目:大数据 来源:网络整理
导读:163. Unique Binary Search Trees 本题难度: Medium Topic: Bit Manipulation Description 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
163. Unique Binary Search Trees
DescriptionGiven n,how many structurally unique BSTs (binary search trees) that store values 1...n? Example 1 3 3 2 1
/ / / 3 2 1 1 3 2
/ / 2 1 2 3
我的代码class Solution:
"""
@param n: An integer
@return: An integer
"""
def numTrees(self,n):
# write your code here
A = [1,1,2]
if n<3:
return A[n]
for i in range(3,n + 1):
res = 0
for j in range(i):
res += A[j]*A[i - j - 1]
A.append(res)
return A[-1]
思路假设f(x)为x个结点的不同的二叉搜索树数目。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
