Description
给你一棵二叉树
Input
输入第一行为该树中结点的个数n,第二行到第n+1行分别为这n个结点的值(也代表序号),左子树和右子树,
Output
输出二叉树的后序遍历
Sample Input
5
1 2 3
2 4 5
3 0 0
4 0 0
5 0 0
Sample Output
4
5
2
3
1
解题思路:先读入数据,放入二维数组,然后循环,如果它不是根结点就开始递归,输出。
程序:
var
a:array[1..100,1..2] of longint;
x,i,y,z,n:longint;
function check(x:longint):boolean;
var
i:longint;
begin
for i:=1 to n do
if (a[i,1]=x) or (a[i,2]=x) then exit(false);
exit(true);
end;
procedure print(x:longint);
begin
if x=0 then exit;
print(a[x,1]);
print(a[x,2]);
writeln(x);
end;
begin
readln(n);
for i:=1 to n do
begin
readln(x,y,z);
a[x,1]:=y;
a[x,2]:=z;
end;
for i:=1 to n do
if check(i) then begin print(i); exit; end;
end.