96-不同的二叉搜索树 发表于 2020-04-01 | 分类于 LeetCode | | 阅读次数: 给定一个整数 n,求以 1 … n 为节点组成的二叉搜索树有多少种? 示例输入: 3输出: 5解释:给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 解法 核心思想:卡特兰数问题,典型的dp解决 class Solution: def numTrees(self, n: int) -> int: dp_list = [0] * (n + 1) dp_list[0] = 1 dp_list[1] = 1 # G(n) = G(0)*G(n-1)+G(1)*(n-2)+...+G(n-1)*G(0) for i in range(2, n + 1): for j in range(i): dp_list[i] += dp_list[j]*dp_list[i-j-1] return dp_list[-1] 相关信息LeetCode:Discussion | Solution -------------本文结束感谢您的阅读------------- 打赏 微信支付 支付宝 Please enable JavaScript to view the comments powered by Disqus.