Scribbling

LeetCode: 96. Unique Binary Search Trees 본문

Computer Science/Coding Test

LeetCode: 96. Unique Binary Search Trees

focalpoint 2021. 10. 13. 20:58
class Solution:
    def numTrees(self, n: int) -> int:
        dp = [0] * (n+1)
        dp[0] = 1
        dp[1] = 1
        for i in range(2, n+1):
            count = 0
            for j in range(1, i+1):
                count += dp[j-1] * dp[i-j]
            dp[i] = count
        return dp[n]