#K5231. Maximum Values at Each Level of a Binary Tree

    ID: 29281 Type: Default 1000ms 256MiB

Maximum Values at Each Level of a Binary Tree

Maximum Values at Each Level of a Binary Tree

Given a binary tree represented by its nodes, determine the maximum value at each level of the tree. The binary tree is provided as follows: the first line contains an integer ( n ) representing the number of nodes. The following ( n ) lines each contain three space-separated integers ( v ), ( l ), and ( r ), corresponding to the node's value, left child, and right child respectively. A child value of ( -1 ) indicates that the child is missing. The tree is rooted at the node with value 1.

You are required to perform a level order (BFS) traversal of the tree and print, for each level, the maximum node value present at that level.

Note: Use the breadth-first search algorithm and output each maximum value in a new line.

inputFormat

The input is given via standard input (stdin) and its format is as follows:

  • The first line contains an integer ( n ), the number of nodes.
  • The next ( n ) lines each contain three space-separated integers ( v ), ( l ), and ( r ) representing a node's value, its left child (or ( -1 ) if none), and its right child (or ( -1 ) if none).

The node with value 1 is always the root of the tree.

outputFormat

For each level of the binary tree, output a single line containing the maximum node value at that level. The output is written to standard output (stdout).## sample

7
1 2 3
2 4 5
3 -1 6
4 -1 -1
5 -1 -1
6 7 -1
7 -1 -1
1

3 6 7

</p>