forked from mission-peace/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnum_trees_preorder.py
More file actions
49 lines (35 loc) · 1.07 KB
/
num_trees_preorder.py
File metadata and controls
49 lines (35 loc) · 1.07 KB
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
=================
Given the number of nodes N, in a pre-order sequence how many unique trees can be created? Number of tree is exactly
same as number of unique BST create with array of size n. The solution is a catalan number.
Complexity
----------
* Dynamic Programming: O(n^2)
* Recursive Solution: O(2^n)
Video
-----
* https://youtu.be/RUB5ZPfKcnY
"""
def num_trees(num_nodes):
T = [0 for _ in range(num_nodes + 1)]
T[0] = 1
T[1] = 1
for n in range(2, num_nodes + 1):
for j in range(0, n):
T[n] += T[j] * T[n - j - 1]
return T[num_nodes]
def num_trees_recursive(num_nodes):
if num_nodes == 0 or num_nodes == 1:
return 1
result = 0
for n in range(1, num_nodes + 1):
result += num_trees_recursive(n - 1) * num_trees_recursive(num_nodes - n)
return result
if __name__ == '__main__':
assert 5 == num_trees(3)
assert 14 == num_trees(4)
assert 42 == num_trees(5)
assert 5 == num_trees_recursive(3)
assert 14 == num_trees_recursive(4)
assert 42 == num_trees_recursive(5)