#P7673. [COCI 2013/2014 #5] OBILAZAK
[COCI 2013/2014 #5] OBILAZAK
Description
You are given a complete binary tree with nodes. The traversal rule of this tree is as follows:
-
The starting point is the only node on the first level of the binary tree.
-
If the left child of the current node has not been recorded yet, record the label of the left child and move to the left child.
-
If the current node has no left child, or its left child has already been recorded, record the label of the current node.
-
If the current node has already been recorded, record the label of the right child and move to the right child.
-
If the current node and both its left and right children have all been recorded, move to the parent of the current node.
Now, you are given the sequence of labels recorded starting from the node on the first level. Please reconstruct the original complete binary tree.
Input Format
The first line contains an integer , meaning that this complete binary tree has nodes.
The second line contains integers, representing the sequence of labels recorded starting from the node on the first level.
Output Format
Output lines in total, representing the original complete binary tree.
2
2 1 3
1
2 3
3
1 6 4 3 5 2 7
3
6 2
1 4 5 7
Hint
Sample Explanation #1

First move to node . Seeing that the left child has not been recorded, move to node and record node . Then, since node has no children, return to node . Seeing that this node has not been recorded, record node . Then, seeing that the right child has not been recorded, move to node and record node .
Sample Explanation #2

Constraints
For of the testdata, .
Notes
The score of this problem follows the original COCI settings. The full score is .
This problem is translated from COCI2013_2014 CONTEST #5 T2 OBILAZAK.
Translated by ChatGPT 5
京公网安备 11011102002149号