Open azl397985856 opened 1 week ago
class Solution: # T: O(n^2) S: O(n) def numTrees(self, n: int) -> int:
DP = [0] * (n+1) # base case DP[0] = DP[1] = 1
DP[0] = 1
DP[1] = 1
for i in range(2, n+1):
for j in range(1, i+1):
leftlen = j - 1
rightlen = i - j
DP[i] += DP[leftlen] * DP[rightlen]
return DP[n]
96. 不同的二叉搜索树
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/unique-binary-search-trees/
前置知识
题目描述
示例:
输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树:
1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3