这篇文章是程序自动发表的,详情可以见
这里
href="http://ounix1xcw.bkt.clouddn.com/github.markdown.css" rel="stylesheet">
href="http://ounix1xcw.bkt.clouddn.com/github.markdown.css" rel="stylesheet">
这是leetcode的第96题--Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 思路 注意到给定一棵树,只有一种对应的BST(1,2,...,n)。所以这道题就使求N个节点能形成多少种不同的树,就很简单了,直接用Catalan数C(n2n)/(1 n),不过在计算中我优化了一下,而且注意最后结果用了round,不能直接用int(),这会截断小数的
show me the code
class Solution(object): def numTrees(self, n): """ :type n: int :rtype: int """ rst = 1 for i in range(2,n 1): rst *=(n i)*1.0/i return int(round(rst))