#C7955. Maximizing Values per Level in a Binary Tree
Maximizing Values per Level in a Binary Tree
Maximizing Values per Level in a Binary Tree
Given a binary tree, compute the maximum value at each level when traversed in level-order (i.e., breadth-first search). Formally, if the levels are denoted by \(L_0, L_1, L_2, \ldots\), for each level \(L_i\) find the maximum node value.
The tree is provided as a space‐separated list in level order, where the string null
represents a missing node. For example, the input 1 3 2 5 3 null 9
corresponds to a tree in which the first level is just 1
, the second level contains 3
and 2
, and the third level contains 5
, 3
(from the left subtree) and 9
(from the right subtree). Your task is to output the maximum value of each level, separated by spaces.
inputFormat
A single line of input containing the level-order traversal of the binary tree. Each node is separated by a space and missing nodes are represented by the string null
.
Example: 1 3 2 5 3 null 9
outputFormat
A single line of output containing the maximum value at each level of the binary tree, separated by spaces. If the tree is empty, output an empty line.## sample
1 3 2 5 3 null 9
1 3 9
</p>