-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path094 Unique Binary Search Trees.py
62 lines (47 loc) · 1.65 KB
/
094 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
"""
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
Author: Rajeev Ranjan
"""
import math
class Solution(object):
def numTrees_math(self, n):
"""
number of unique binary search tree
Catalan Number
C_n = {2n\choose n} - {2n\choose n+1}
Proof: http://en.wikipedia.org/wiki/Catalan_number#Second_proof
:param n: integer
:return: integer
"""
return math.factorial(2*n)/(math.factorial(n)*math.factorial(n))-math.factorial(2*n)/(
math.factorial(n+1)*math.factorial(n-1))
def numTrees(self, n):
"""
number of unique binary search tree
dp
dp[i] means # BST constructed from [1...i]
dp[3] = dp[0]*dp[2] # 1 as pivot
+dp[1]*dp[1] # 2 is pivot
+dp[2]*dp[0] # 3 as pivot
follow the 2nd proof of Catalan number.
Proof: http://en.wikipedia.org/wiki/Catalan_number#First_proof
:param n: integer
:return: integer
"""
if n < 2:
return n
dp = [0 for _ in xrange(n+1)]
dp[0] = 1
for i in xrange(1, n+1):
for j in xrange(i):
dp[i] += dp[j]*dp[i-j-1]
return dp[-1]
if __name__ == "__main__":
assert Solution().numTrees(100) == Solution().numTrees_math(100)