1 条题解
-
0
C++ :
#include<cstdio> int l[1000],r[1000],f[1000]; int dfs(int n) { if(n==f[n]) return n; return dfs(f[n]); } void xianxu(int k) { printf("%d ",k); if(l[k]) xianxu(l[k]); if(r[k]) xianxu(r[k]); } void zhongxu(int k) { if(l[k]) zhongxu(l[k]); printf("%d ",k); if(r[k]) zhongxu(r[k]); } void houxu(int k) { if(l[k]) houxu(l[k]); if(r[k]) houxu(r[k]); printf("%d ",k); } int main() { int i,n,a,b,c; scanf("%d",&n); for(i=1;i<=n;i++) f[i]=i; for(i=1;i<=n;i++) { scanf("%d%d%d",&a,&b,&c); l[a]=b,r[a]=c; f[b]=f[c]=a; } int root=dfs(n); printf("%d\n",root); xianxu(root); printf("\n"); zhongxu(root); printf("\n"); houxu(root); printf("\n"); return 0; }
Pascal :
program p30; type tree=record father:integer; left:integer; right:integer; end; var a:array[1..1000] of tree; n,i,root,f,l,r,t:integer; procedure first(t:integer); begin if t<>0 then begin write(t,' '); first(a[t].left); first(a[t].right); end; end; procedure second(t:integer); begin if t<>0 then begin second(a[t].left); write(t,' '); second(a[t].right); end; end; procedure third(t:integer); begin if t<>0 then begin third(a[t].left); third(a[t].right); write(t,' '); end; end; begin readln(n); for i:=1 to n do begin readln(f,l,r); if l<>0 then begin a[l].father:=f; a[f].left:=l; end; if r<>0 then begin a[r].father:=f; a[f].right:=r; end; end; for i:=1 to n do if a[i].father=0 then root:=i; writeln(root); first(root); writeln; second(root); writeln; third(root); end.
- 1
信息
- ID
- 921
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者