#C1653. Average of Levels in Binary Tree

    ID: 44882 Type: Default 1000ms 256MiB

Average of Levels in Binary Tree

Average of Levels in Binary Tree

Given a binary tree represented in level-order as a list, compute the average value of the nodes on each level.

More formally, given a binary tree, let (L_i) denote the (i)-th level of the tree (with the root being level 1). For each level, compute the average value defined as (\frac{\text{sum of values in } L_i}{\text{number of nodes in } L_i}). Return these averages as a list of floats.

Note: The tree is input as a string in the format: [node1,node2,...,nodeN] where 'null' represents a missing node. For example, the tree described by [3,9,20,null,null,15,7] corresponds to the binary tree:

         3
        / \
       9  20
         /  \
        15   7

The expected output for this tree is: 3.0 14.5 11.0 (each average printed on the same line separated by spaces).

inputFormat

A single line string representing a binary tree in level-order format. The tree is enclosed in square brackets and nodes are separated by commas. Use the token 'null' to denote missing nodes. For example: [3,9,20,null,null,15,7]

outputFormat

Print the average of the node values at each level of the binary tree. The averages should be printed in order from the root level to the deepest level, separated by a single space. If the tree is empty (i.e. input is []), output nothing.## sample

[3,9,20,null,null,15,7]
3.0 14.5 11.0