描述
给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?
样例
给出n = 3,有5种不同形态的二叉查找树:
class Solution:
"""
@param n: An integer
@return: An integer
"""
def numTrees(self, n):
# write your code here
ans = [0] * (n+1)
ans[0] = 1
for i in range(1, n+1):
for j in range(0, i):
ans[i] += ans[j]*ans[i-j-1]
return ans[n]