/ OPS / 题库 /

Tree Reorder

Tree Reorder

#描述#
<p class=MsoNormal><font size=3 face="Times New Roman"><span lang=EN-US
style='font-size:12.0pt'>Maybe you have taken the course called Data Structure
before. You should be familiar with the fact that no parenthesis is needed in
the suffix expression. But have you ever wondered why?<o:p>/o:p</span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span lang=EN-US
style='font-size:12.0pt'>We know that an expression can be uniquely represented
by a binary tree. The suffix expression is the post-order traversal of this
tree. Given the post-order traversal of a binary tree, each of whose non-leaf
nodes has exactly two children, we can reconstruct the original binary tree if
we are told which nodes in the post-order traversal are non-leaves. This means
that no parenthesis is needed in the suffix expression.<o:p>/o:p</span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span lang=EN-US
style='font-size:12.0pt'>Here is your task. Given a string S which represents
the post-order traversal of a binary tree T in which all of the non-leaf nodes
have exactly two children, and given which nodes in S are non-leaves, you must
reconstruct the binary tree and output the in-order traversal of T.<o:p>/o:p</span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span lang=EN-US
style='font-size:12.0pt'>Each node of T has a label, which is a <span
class=GramE>letter(</span>‘a’-‘z’, ‘A’-‘Z’). A lowercase letter(‘a’-’z’) means
the corresponding node is a leaf and an uppercase letter(‘A’-Z’) means the
corresponding node is a non-leaf.<o:p>/o:p</span></font></p>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0cm;margin-bottom:6.0pt;
margin-left:0cm'><b style='mso-bidi-font-weight:normal'><font size=3
face="Times New Roman"><span lang=EN-US style='font-size:12.0pt;font-weight:
bold;mso-bidi-font-weight:normal'>Notes<o:p>/o:p</span></font></b></p>

<p class=MsoNormal style='margin-left:21.0pt;text-indent:-21.0pt;mso-list:l1 level1 lfo2;
tab-stops:list 21.0pt'><![if !supportLists]><font size=1 face=Wingdings><span
lang=EN-US style='font-size:9.0pt;font-family:Wingdings;mso-fareast-font-family:
Wingdings;mso-bidi-font-family:Wingdings'><span style='mso-list:Ignore'>l<font
size=1 face="Times New Roman"><span style='font:7.0pt "Times New Roman"'>         
</span></font></span></span></font><![endif]><font size=3><span lang=EN-US
style='font-size:12.0pt'>The post-order traversal of a binary tree is to visit
the left subtree of the root first, then the right subtree and finally the
root.<o:p>/o:p</span></font></p>

<p class=MsoNormal style='margin-left:21.0pt;text-indent:-21.0pt;mso-list:l1 level1 lfo2;
tab-stops:list 21.0pt'><![if !supportLists]><font size=1 face=Wingdings><span
lang=EN-US style='font-size:9.0pt;font-family:Wingdings;mso-fareast-font-family:
Wingdings;mso-bidi-font-family:Wingdings'><span style='mso-list:Ignore'>l<font
size=1 face="Times New Roman"><span style='font:7.0pt "Times New Roman"'>         
</span></font></span></span></font><![endif]><font size=3><span lang=EN-US
style='font-size:12.0pt'>The in-order traversal of a binary tree is to visit
the left subtree of the root first, then the root, and finally the right
subtree.<o:p>/o:p</span></font></p>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0cm;margin-bottom:6.0pt;
margin-left:0cm'><b style='mso-bidi-font-weight:normal'><font size=3
face="Times New Roman"><span lang=EN-US style='font-size:12.0pt;font-weight:
bold;mso-bidi-font-weight:normal'>Constraints<o:p>/o:p</span></font></b></p>

<p class=MsoNormal style='margin-left:21.0pt;text-indent:-21.0pt;mso-list:l0 level1 lfo4;
tab-stops:list 21.0pt'><![if !supportLists]><font size=1 face=Wingdings><span
lang=EN-US style='font-size:9.0pt;font-family:Wingdings;mso-fareast-font-family:
Wingdings;mso-bidi-font-family:Wingdings'><span style='mso-list:Ignore'>l<font
size=1 face="Times New Roman"><span style='font:7.0pt "Times New Roman"'>         
</span></font></span></span></font><![endif]><font size=3><span lang=EN-US
style='font-size:12.0pt'>S contains <span class=GramE>letters(</span>‘a’-‘z’,
‘A’-‘Z’) only and don’t contain any white spaces.<o:p>/o:p</span></font></p>

<p class=MsoNormal style='margin-left:21.0pt;text-indent:-21.0pt;mso-list:l0 level1 lfo4;
tab-stops:list 21.0pt'><![if !supportLists]><font size=1 face=Wingdings><span
lang=EN-US style='font-size:9.0pt;font-family:Wingdings;mso-fareast-font-family:
Wingdings;mso-bidi-font-family:Wingdings'><span style='mso-list:Ignore'>l<font
size=1 face="Times New Roman"><span style='font:7.0pt "Times New Roman"'>         
</span></font></span></span></font><![endif]><font size=3><span lang=EN-US
style='font-size:12.0pt'>The length of S is between 3 and 999, inclusive.<o:p>/o:p</span></font></p>

<p class=MsoNormal style='margin-left:21.0pt;text-indent:-21.0pt;mso-list:l0 level1 lfo4;
tab-stops:list 21.0pt'><![if !supportLists]><font size=1 face=Wingdings><span
lang=EN-US style='font-size:9.0pt;font-family:Wingdings;mso-fareast-font-family:
Wingdings;mso-bidi-font-family:Wingdings'><span style='mso-list:Ignore'>l<font
size=1 face="Times New Roman"><span style='font:7.0pt "Times New Roman"'>         
</span></font></span></span></font><![endif]><font size=3><span lang=EN-US
style='font-size:12.0pt'>The length of S is an odd number.<o:p>/o:p</span></font></p>

<p class=MsoNormal style='margin-left:21.0pt;text-indent:-21.0pt;mso-list:l0 level1 lfo4;
tab-stops:list 21.0pt'><![if !supportLists]><font size=1 face=Wingdings><span
lang=EN-US style='font-size:9.0pt;font-family:Wingdings;mso-fareast-font-family:
Wingdings;mso-bidi-font-family:Wingdings'><span style='mso-list:Ignore'>l<font
size=1 face="Times New Roman"><span style='font:7.0pt "Times New Roman"'>         
</span></font></span></span></font><![endif]><font size=3><span lang=EN-US
style='font-size:12.0pt'>S will be the post-order traversal of a binary tree.<o:p>/o:p</span></font></p>

#格式#
##输入格式##
The input contains an integer on the first line, which indicates the number of test cases. Each test case contains one string S on a line, the post-order traversal of a binary tree.

##输出格式##
For each test case, output on a line one string which is the in-order traversal of the corresponding binary tree. There can be no white spaces in your output.

#样例1#
##样例输入1##

2
bcA
efBghCA

##样例输出1##

bAc
eBfAgCh

#限制#
1000ms
32768KB

#提示#

#来源#
lily

信息

ID
1399
难度
5
分类
category1 点击显示
标签
递交数
0
已通过
0
通过率
?
上传者