#K73327. Connect Level Order Siblings

    ID: 33951 Type: Default 1000ms 256MiB

Connect Level Order Siblings

Connect Level Order Siblings

Given a binary tree, connect each node with its level order successor such that all nodes on the same level are linked from left to right. The last node on any level should have its next pointer set to null. The binary tree is provided in level order where the string 'null' represents an absent node. After performing the linking, you are required to output the next pointer for each node in the tree (in level order), where a null next pointer is represented by 0.

For example, consider the binary tree given by the level order traversal: 1 2 3 4 5 6 7. After connecting sibling nodes, the pointers will be set as follows:

Level 1: 1 -> null Level 2: 2 -> 3 -> null Level 3: 4 -> 5 -> 6 -> 7 -> null

Thus, the output should list the next values in level order: 0 3 0 5 6 7 0 (since 1's next is null (0), 2's next points to 3, 3's next is null (0), etc.).

Note: All formulas (if any) should be given in LaTeX format. In this problem no explicit formulas are required.

inputFormat

The input consists of a single line containing space-separated values representing the nodes of a binary tree in level order. Use the string "null" to denote that a node is missing. For example:

1 2 3 4 5 6 7

or

1 2 3 4 5 null 7

outputFormat

Print a single line of space-separated integers. For each node (in level order), output the value of its next pointer after the linking. If the next pointer is null, output 0.## sample

1
0

</p>