-
Notifications
You must be signed in to change notification settings - Fork 4
/
treeDiameter.py
61 lines (49 loc) · 1.49 KB
/
treeDiameter.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
from Tree.commons import insert, print_tree, is_leaf
from Tree.treeHeight import calculate_height
# O(n^2)
def calculate_diameter_v1(root):
if root == None:
return 0
lh = calculate_height(root.left)
rh = calculate_height(root.right)
diameter_via_height = lh + rh + 1
ld = calculate_diameter_v1(root.left)
rd = calculate_diameter_v1(root.right)
diameter_via_left_right_subtree = ld + rd
return max(diameter_via_height, diameter_via_left_right_subtree)
# O(n)
def calculate_diameter_v2(root):
if root == None:
return 0
lh = calculate_diameter_v2(root.left)
rh = calculate_diameter_v2(root.right)
calculate_diameter_v2.d = max(calculate_diameter_v2.d, lh + rh + 1)
return max(lh, rh) + 1
# Driver program to test above function
if __name__ == "__main__":
""" Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80
/ \
15 25
"""
root = None
root = insert(root, 50)
insert(root, 30)
insert(root, 20)
insert(root, 15)
insert(root, 25)
insert(root, 40)
insert(root, 70)
insert(root, 60)
insert(root, 80)
print("\n----- Diameter via V1 -----\n")
d = calculate_diameter_v1(root)
print("Diameter is {diameter}".format(diameter=d))
print("\n----- Diameter via V2 -----\n")
calculate_diameter_v2.d = -1
d = calculate_diameter_v2(root)
print("Diameter is {diameter}".format(diameter=calculate_diameter_v2.d))