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
1
2
4
5
3
解题思路:先读入数据,放入二维数组,然后循环,如果它不是根结点就开始输出,递归。
程序:
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;
writeln(x);
print(a[x,1]);
print(a[x,2]);
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.
版权属于:Chris
原文地址:http://blog.sina.com.cn/s/blog_83ac6af80102v0p9.html
转载时必须以链接形式注明原始出处及本声明。