-
Notifications
You must be signed in to change notification settings - Fork 1
/
96.unique-binary-search-trees.py
65 lines (62 loc) · 1.07 KB
/
96.unique-binary-search-trees.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#
# @lc app=leetcode id=96 lang=python3
#
# [96] Unique Binary Search Trees
#
# https://leetcode.com/problems/unique-binary-search-trees/description/
#
# algorithms
# Medium (59.62%)
# Likes: 8901
# Dislikes: 353
# Total Accepted: 560K
# Total Submissions: 939.3K
# Testcase Example: '3'
#
# Given an integer n, return the number of structurally unique BST's (binary
# search trees) which has exactly n nodes of unique values from 1 to n.
#
#
# Example 1:
#
#
# Input: n = 3
# Output: 5
#
#
# Example 2:
#
#
# Input: n = 1
# Output: 1
#
#
#
# Constraints:
#
#
# 1 <= n <= 19
#
#
#
# @lc code=start
class Solution:
def numTrees(self, n: int) -> int:
if n == 0:
return 0
if n == 1:
return 1
if n == 2:
return 2
if n == 3:
return 5
dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 1
dp[2] = 2
dp[3] = 5
for i in range(4, n + 1):
for j in range(1, i + 1):
dp[i] += dp[j - 1] * dp[i - j]
return dp[n]
# @lc code=end