#K49852. Largest Value in Each Tree Row

    ID: 28734 Type: Default 1000ms 256MiB

Largest Value in Each Tree Row

Largest Value in Each Tree Row

You are given a binary tree represented in level order as a list. The tree is serialized in the following format: [node1, node2, ...]. The value 'null' indicates that there is no node present at that position. Your task is to find the largest value in each row of the binary tree and output these values in a list.

For example, the tree represented by [1, 3, 2, 5, 3, 9, 6] corresponds to a tree with three levels, and the largest values in each row are [1, 3, 9].

The relation between levels and the tree structure can be mathematically expressed as follows: [ \text{Let } L_i \text{ be the set of nodes in the } i^\text{th} \text{ row, then } answer[i] = \max_{x \in L_i} x. ]

inputFormat

Input is read from standard input (stdin) as a single line. The line contains a serialized binary tree in level order. The format is a list enclosed in square brackets, with values separated by commas. For missing nodes, the value is given as 'null'.

Examples: • [1,3,2,5,3,9,6] • [10] • []

outputFormat

Output the list of largest values for each row of the binary tree to standard output (stdout). The output should be printed as a list in the same format as the input, for example: [1,3,9].## sample

[1,3,2,5,3,9,6]
[1,3,9]