#abc255f. F - Pre-order and In-order
F - Pre-order and In-order
Score : points
问题陈述
考虑一个具有 个顶点的 二叉树,顶点编号为 。此处,二叉树是一个根树,其中每个顶点最多有两个子节点。具体来说,二叉树中的每个顶点至多有一个 左孩子 和至多有一个 右孩子。
确定是否存在一棵以顶点 为根的二叉树满足以下条件,并在存在时给出这样的树。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
Consider a binary tree with vertices numbered . Here, a binary tree is a rooted tree where each vertex has at most two children. Specifically, each vertex in a binary tree has at most one left child and at most one right child.
Determine whether there exists a binary tree rooted at Vertex satisfying the conditions below, and present such a tree if it exists.
- The depth-first traversal of the tree in pre-order is .
- The depth-first traversal of the tree in in-order is .
Constraints
- is an integer.
- is a permutation of .
- is a permutation of .
Input
Input is given from Standard Input in the following format:
Output
If there is no binary tree rooted at Vertex satisfying the conditions in Problem Statement, print .
Otherwise, print one such tree in lines as follows. For each , the -th line should contain and , the indices of the left and right children of Vertex , respectively. Here, if Vertex has no left (right) child, () should be .
If there are multiple binary trees rooted at Vertex satisfying the conditions, any of them will be accepted.
Sample Input 1
6
1 3 5 6 4 2
3 5 1 4 6 2
Sample Output 1
3 6
0 0
0 5
0 0
0 0
4 2
The binary tree rooted at Vertex shown in the following image satisfies the conditions.
Sample Input 2
2
2 1
1 2
Sample Output 2
-1
No binary tree rooted at Vertex satisfies the conditions, so should be printed.
update @ 2024/3/10 10:52:11