给定正整数 n,求以 1~n 为节点组成的不同的二叉查找树有多少种?
参考代码:
class Solution: def numTrees(self, n): dp = [1, 1, 2] if n <= 2: return dp[n] else: dp += [0 for i in range(n-2)] for i in range(1, n+1): for j in range(1, i+1): dp[1] += dp[j - 1] * dp[i-j] return dp[n]