forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiameter_of_binary_tree.py
74 lines (54 loc) · 1.68 KB
/
diameter_of_binary_tree.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
63
64
65
66
67
68
69
70
71
72
73
74
"""
The diameter/width of a tree is defined as the number of nodes on the longest path between two end nodes.
"""
# A binary tree Node
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# utility class to pass height object
class Height:
def __init__(self):
self.h = 0
# Function to calculate diameter of the given binary tree
def calculate_diameter(root: Node, height: int) -> int:
# to store height of left and right subtree
lh = Height()
rh = Height()
# base condition- when binary tree is empty
if root is None:
height.h = 0
return 0
# ldiameter --> diameter of left subtree
# rdiameter --> diameter of right subtree
# height of left subtree and right subtree is obtained from lh and rh and returned value of function is stored in ldiameter and rdiameter
ldiameter = calculate_diameter(root.left, lh)
rdiameter = calculate_diameter(root.right, rh)
# height of tree will be max of left subtree
# height and right subtree height plus1
height.h = max(lh.h, rh.h) + 1
# return maximum of the following
# 1)left diameter
# 2)right diameter
# 3)left height + right height + 1
return max(lh.h + rh.h + 1, max(ldiameter, rdiameter))
def diameter(root: Node) -> int:
height = Height()
return calculate_diameter(root, height)
if __name__ == "__main__":
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
"""
Constructed binary tree is
1
/ \
2 3
/ \
4 5
"""
print("The diameter of the binary tree is:", end=" ")
print(diameter(root))