forked from mission-peace/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnum_bst.py
More file actions
49 lines (33 loc) · 949 Bytes
/
num_bst.py
File metadata and controls
49 lines (33 loc) · 949 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
43
44
45
46
47
48
49
"""
Problem Statement
=================
Count number of binary search trees created for array of size n. The solution is the nth catalan number.
Complexity
----------
* Dynamic Programming: O(n^2)
* Recursive Solution: O(2^n)
Video
-----
* https://youtu.be/YDf982Lb84o
Reference
---------
* http://www.geeksforgeeks.org/program-nth-catalan-number/
"""
def num_bst(num_nodes):
T = [0 for _ in range(num_nodes + 1)]
T[0] = 1
T[1] = 1
for node in range(2, num_nodes+1):
for sub in range(0, node):
T[node] += T[sub] * T[node - sub - 1]
return T[num_nodes]
def num_bst_recursive(num_nodes):
if num_nodes == 0 or num_nodes == 1:
return 1
result = 0
for root in range(1, num_nodes + 1):
result += num_bst_recursive(root - 1) * num_bst_recursive(num_nodes - root)
return result
if __name__ == '__main__':
assert 5 == num_bst(3)
assert 5 == num_bst_recursive(3)