-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path96.py
45 lines (40 loc) · 1.16 KB
/
96.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
'''
@Date: 2020-07-31 14:45:03
@LastEditors: FrankCast1e
@LastEditTime: 2020-07-31 15:26:12
@FilePath: /LeetCode/96.py
'''
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
# class Solution:
# def numTrees(self, n: int) -> int:
# nums = [i+1 for i in range(n)]
# all_num = self.build_search_tree(nums)
# return all_num
# def build_search_tree(self, nums):
# if nums == []:
# return 1
# count = 0
# for i in range(len(nums)):
# left_count = self.build_search_tree(nums[:i])
# right_count = self.build_search_tree(nums[i+1:])
# count += left_count*right_count
# return count
# Catalan Number
class Solution:
def numTrees(self, n: int) -> int:
memo = []
memo.append(1)
memo.append(1)
for i in range(2,n+1):
g_n = 0
for j in range(i):
g_n += memo[j]*memo[i-1-j]
memo.append(g_n)
return memo[-1]
x = Solution()
print(x.numTrees(4))