Files
PAT/html/t1006.html
T

36 lines
1.5 KiB
HTML

<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<h1>1006. Tree Traversals - Hard Version (35)</h1>
<div id="problemContent">
<p>Given the partial results of a binary tree's traversals in in-order, pre-order, and post-order. You are supposed to output the complete results and the level order traversal sequence of the corresponding tree.</p>
<p><b>
Input Specification:
</b></p>
<p>Each input file contains one test case. For each case, a positive integer N (&lt;=100) is given in the first line. Then three lines follow, containing the incomplete in-order, pre-order and post-order traversal sequences, respectively. It is assumed that the tree nodes are numbered from 1 to N and no number is given out of the range. A "-" represents a missing number.</p>
<p><b>
Output Specification:
</b></p>
<p>For each case, print in four lines the complete in-order, pre-order and post-order traversal sequences, together with the level order traversal sequence of the corresponding tree. The numbers must be separated by a space, and there must be no extra space at the end of each line. If it is impossible to reconstruct the unique tree from the given information, simply print "Impossible".
</p>
<b>Sample Input 1:</b><pre>
9
3 - 2 1 7 9 - 4 6
9 - 5 3 2 1 - 6 4
3 1 - - 7 - 6 8 -
</pre>
<b>Sample Output 1:</b><pre>
3 5 2 1 7 9 8 4 6
9 7 5 3 2 1 8 6 4
3 1 2 5 7 4 6 8 9
9 7 8 5 6 3 2 4 1
</pre>
<b>Sample Input 2:</b><pre>
3
- - -
- 1 -
1 - -
</pre>
<b>Sample Output 2:</b><pre>
Impossible
</pre>
</div>