I. 2023春-A-4 Tree of Love

    传统题 400ms 64MiB

2023春-A-4 Tree of Love

该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。

题目描述

If a binary tree has its left and right subtrees perfectly symmetric. And more, if from left to right, the depths of leaves are first in increasing (or non-decreasing) then decreasing (or non-increasing), then again increasing (or non-decreasing), and finally decreasing (or non-increasing) order, then the shape of this tree looks like a heart (as shown by the above figure), and hence is called "Tree of Love".

Given the inorder and postorder traversal sequences of a binary tree, your job is to construct this tree and test if it is a tree of love, and output its outer contour(外轮廓). "Outer contour" consists of nodes visited from the root, along the left most path to a leaf, then all the leaves from left to right, and finally back to the root along the right most path.

输入格式

Each input file contains one test case. For each case, the first line gives a positive integer NN (<100< 100), which is the number of nodes in the tree. Then the next two lines each contains NN positive keys as the inorder and postorder traversal sequences, respectively. All the keys are distinct integers no more than 10310^3. The numbers in a line are separated by spaces. It is guaranteed that a unique binary tree can be constructed from the input.

输出格式

For each test case, if the tree is a tree of love, output Yes in the first line, or No if not. Then output the outer contour in the second line.

All the numbers in a line must be separated by 1 space, and there must be no extra space at the beginning or the end of the line.

样例

27
5 4 6 22 3 23 7 20 2 21 8 18 9 1 10 19 11 24 17 25 12 26 16 27 13 15 14
5 6 22 4 7 23 20 3 8 21 9 18 2 10 11 24 19 12 26 25 13 27 14 15 16 17 1
Yes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
11
4 8 10 2 5 1 6 3 9 11 7
10 8 4 5 2 6 11 9 7 3 1
No
1 2 4 10 5 6 11 7 3

提示

The answer is No because the tree is not symmetric. It would be Yes if we swap 9 and 11 in the inorder sequence.

限制

对于所有的测试用例,限制为400 ms, 64 MB

PAT2023春季重现赛

未参加
状态
已结束
规则
IOI
题目
9
开始于
2025-11-30 18:30
结束于
2025-11-30 22:00
持续时间
3.5 小时
主持人
参赛人数
71