力扣 96. 不同的二叉搜索树

动态规划

动态规划

1
2
3
4
5
6
7
8
9
def numTrees(n):
if n <= 1:
return 1
dp = [0] * (n + 1)
dp[0], dp[1] = 1, 1
for i in range(2, n + 1): # 长度
for j in range(1, i + 1): # 根位置
dp[i] += dp[j - 1] * dp[i - j]
return dp[-1]
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信