Description
由于先序、中序和后序中任一个都不能确定一棵二叉树,所以对二叉树做如下处理,将二叉树的空结点用.补齐,如图所示。我们把这样处理后的二叉树称为原二叉树的扩展二叉树,扩展二叉树的先序和后序序列能唯一确定其二叉树。
现给出扩展二叉树的先序序列,要求出其中中序和后序序列。
Input
Output
Sample Input
ABD..EF..G..C..
Sample Output
DBFEGAC
DFGEBCA
解题思路:先用指针变量和线性链表建立一个二叉树,然后用递归分别输出中序序列和后续序列即可。
程序:
type
tree=^node;
node=record
a:char;
lchild,rchild:tree;
end;
var
s:string;
i:longint;
t:tree;
procedure build(var t:tree);
begin
inc(i);
if s[i]<>'.' then
begin
new(t);
t^.a:=s[i];
build(t^.lchild);
build(t^.rchild);
end
else t:=nil;
end;
procedure printzx(t:tree);
begin
if t<>nil then
begin
printzx(t^.lchild);
write(t^.a);
printzx(t^.rchild);
end;
end;
procedure printhx(t:tree);
begin
if t<>nil then
begin
printhx(t^.lchild);
printhx(t^.rchild);
write(t^.a);
end;
end;
begin
readln(s);
i:=0;
build(t);
printzx(t);
writeln;
printhx(t);
end.