-
Notifications
You must be signed in to change notification settings - Fork 40
/
level_with_max_width.py
73 lines (61 loc) · 1.64 KB
/
level_with_max_width.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
# Date: 2020-09-26
#
# Description:
# Find the level in tree which has maximum nodes/width.
#
# Approach:
# Do a level order traversal and add all nodes below the current level in
# queue. In each traversal pop all elements currently present in the queue so
# that queue is left with elements that are present just below current level.
# We can take the count of number of nodes present currently in queue and
# compare with previous result, if smaller we can update result.
#
# Complexity:
# O(n), n = number of nodes in tree.
import collections
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def level_with_max_width(root):
if root is None:
return (0, 0)
q = collections.deque([root])
max_nodes = 0
max_node_level = 0
level = -1
while q:
level += 1
size = len(q)
if max_nodes < size:
max_node_level = level
max_nodes = size
while size:
size -= 1
node = q.popleft()
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
return (max_nodes, max_node_level)
def main():
# Level 0
root = Node(1)
# Level 1
root.left = Node(2)
root.right = Node(3)
# Level 2
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
# Level 3
root.left.left.left = Node(8)
max_nodes, level = level_with_max_width(root)
print('Max nodes: %d at level: %d' % (max_nodes, level))
if __name__ == '__main__':
main()
# Output
# ------
# Max nodes: 4 at level: 2