-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumBST.py
More file actions
42 lines (27 loc) · 925 Bytes
/
NumBST.py
File metadata and controls
42 lines (27 loc) · 925 Bytes
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
'''
https://leetcode.com/problems/unique-binary-search-trees/description/
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'''
class Solution:
def numTrees(self, n: int) -> int:
mem_func = [-1]*(n+1)
mem_func[0] = 1
return self.numTreesUtil(mem_func,n)
def numTreesUtil(self, mem_func : list[int], n : int):
if mem_func[n] == -1:
res = 0
for i in range(0, n):
res = res + self.numTreesUtil(mem_func, i) * self.numTreesUtil(mem_func, n-1-i)
mem_func[n] = res
return mem_func[n]
if __name__ == "__main__":
s = Solution()
for i in range(1, 20):
print(s.numTrees(i))