637. Average of Levels in Binary Tree
https://leetcode.com/problems/average-of-levels-in-binary-tree/
Python
class Solution:
def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
def travel(node, level: int, result: list):
if not node:
return
if level == len(result):
result.append([1, node.val])
else:
result[level][0] += 1
result[level][1] += node.val
travel(node.left, level+1, result)
travel(node.right, level+1, result)
result = [[1, float(root.val)]]
travel(root, 0, result)
return [n[1]/n[0] for n in result]