637. Average of Levels in Binary Tree
Question
Given the
root
of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within10<sup>-5</sup>
of the actual answer will be accepted.
Solution
BFS搜索,记录单层的总和sum和单层的个数count。
每次遍历一个层级的所有节点,并更新sum和count。
遍历完毕后将当层级的平均数加入列表,同时将sum和count清零。
Code
1 | /** |
637. Average of Levels in Binary Tree
https://xuanhe95.github.io/2022/09/02/637-Average-of-Levels-in-Binary-Tree/