Grokking the Coding Interview: The Original Course
Vote

0% completed

​

Solution: Level Averages in a Binary Tree

Problem Statement

Given a binary tree, populate an array to represent the averages of all of its levels.

Example 1:

Example 2:

Constraints:

  • The number of nodes in the tree is in the range [1, 10<sup>4</sup>].
  • -2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1

Why this is a Tree Level Order Traversal problem

What the question saysThe signal it matches
"populate an array to represent the averages of all of its levels"you need one answer per level

.....

.....

.....

Like the course? Get enrolled and start learning!
H

hahn.zachary

· 19 days ago

I know the following feedback is a bit of a nitpick, but since the solution explanation page explicitly calls out numeric data type size constraints as a potential issue, I figure the Python 3 solution implementation should at least follow its own advice.

Python 3's int supports arbitrarily large integers (constrained only by available memory). However, Python 3 does not support arbitrarily large float values. The Python solution page warns against using too small of a numeric data type to store the running total of each level, but then uses a float type for this value. I'm assuming a float was used as a precaution against truncation of the levelSum / levelSize calculation (float divided by an int). However, unlike some other languages, Python's / operator

Reading Progress

0%


Vote for new content