Pro.ID21730 TitleRECONSTRUCTING TREES Title链接http://10.20.2.8/oj/exercise/problem?problem_id=21730 AC0 Submit0 Ratio- 时间&空间限制描述It is very easy to produce the various traversals of a binary tree, however this problem requires you to produce the tree from the traversals. Specifically, given the pre-order and in-order traversals of a binary tree (not necessarily a binary search tree), reconstruct, if possible, the original tree.
For instance, the following tree will produce the traversals shown
E
/ \
D F
/ \
B G
/ \ \
A C I
/ \
H K
/
J
Preorder : EDBACFGIHKJ
Inorder : ABCDEFGHIJK
Postorder: ACBDHJKIGFE 输入Input will consist of representations of several trees, each on a single line, and terminated by a #. Each line will consist of two strings of up to 26 unique uppercase letters in the form
<pre-order> <space> <in-order>.
You can assume that the two strings will be permutations of each other. 输出Description It is very easy to produce the various traversals of a binary tree, however this problem requires you to produce the tree from the traversals. Specifically, given the pre-order and in-order traversals of a binary tree (not necessarily a binary search tree), reconstruct, if possible, the original tree.
For instance, the following tree will produce the traversals shown
E
/ \
D F
/ \
B G
/ \ \
A C I
/ \
H K
/
J
Preorder : EDBACFGIHKJ
Inorder : ABCDEFGHIJK
Postorder: ACBDHJKIGFE Input Input will consist of representations of several trees, each on a single line, and terminated by a #. Each line will consist of two strings of up to 26 unique uppercase letters in the form
<pre-order> <space> <in-order>.
You can assume that the two strings will be permutations of each other. Output Output will consist of the post-order traversal of the reconstructed tree if possible, otherwise the words 'Invalid tree'. Sample Input EDBACFGIHKJ ABCDEFGHIJK
# Sample Output ACBDHJKIGFE Source 样例输入EDBACFGIHKJ ABCDEFGHIJK
# 样例输出ACBDHJKIGFE 作者 |