forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 15
/
unique-binary-search-trees.py
51 lines (46 loc) · 1.28 KB
/
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
from __future__ import print_function
# Time: O(n)
# Space: O(1)
#
# Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
#
# For example,
# Given n = 3, there are a total of 5 unique BST's.
#
# 1 3 3 2 1
# \ / / / \ \
# 3 2 1 1 3 2
# / / \ \
# 2 1 2 3
#
# Math solution.
class Solution(object):
def numTrees(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0:
return 1
def combination(n, k):
count = 1
# C(n, k) = (n) / 1 * (n - 1) / 2 ... * (n - k + 1) / k
for i in xrange(1, k + 1):
count = count * (n - i + 1) / i;
return count
return combination(2 * n, n) - combination(2 * n, n - 1)
# Time: O(n^2)
# Space: O(n)
# DP solution.
class Solution2:
# @return an integer
def numTrees(self, n):
counts = [1, 1]
for i in xrange(2, n + 1):
count = 0
for j in xrange(i):
count += counts[j] * counts[i - j - 1]
counts.append(count)
return counts[-1]
if __name__ == "__main__":
print(Solution().numTrees(3))