-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaverage-of-levels-in-binary-tree.py
84 lines (74 loc) · 2.89 KB
/
average-of-levels-in-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
75
76
77
78
79
80
81
82
83
84
# 637. Average of Levels in Binary Tree
# 🟢 Easy
#
# https://leetcode.com/problems/average-of-levels-in-binary-tree/
#
# Tags: Tree - Depth-First Search - Breadth-First Search - Binary Tree
import timeit
from collections import deque
from typing import List, Optional
from data import deserializeStringArrayToBinaryTree
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
# This problem can be solved using breadth first search and computing the
# average of each level as we process them.
#
# Time complexity: O(n) - We visit each node of the tree once.
# Space complexity: O(n) - The queue will hold at most one level at a
# time, one level can hold 2 times more nodes than the previous level
# and it can hold more than half of all the nodes in the tree > O(n/2).
#
# Runtime: 50 ms, faster than 96.56%
# Memory Usage: 16.5 MB, less than 87.46%
class IterativeBFS:
def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
# Use a queue for BFS. Root cannot be null.
q = deque([root])
# Use an empty list to store the average of each level, initially
# we have no way of knowing how many levels there will be.
result = []
while q:
# Store the sum of nodes in this level.
temp, level_length = 0, len(q)
# Iterate over all elements in one level.
for _ in range(len(q)):
# Pop the leftmost element.
current = q.popleft()
# Add this element value to the level sum.
temp += current.val
# Append existing children to the right.
if current.left:
q.append(current.left)
if current.right:
q.append(current.right)
# Calculate the average of this level.
result.append(temp / level_length)
return result
def test():
executors = [IterativeBFS]
tests = [
["[3,9,20,null,null,15,7]", [3.00000, 14.50000, 11.00000]],
["[3,9,20,15,7]", [3.00000, 14.50000, 11.00000]],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
root = deserializeStringArrayToBinaryTree(t[0])
result = sol.averageOfLevels(root)
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()