Aizu ALDS1_7_C Tree Walk
#include<bits/stdc++.h>
using namespace std;
#define NIL -1
#define MAX 30
struct Node{
int p,l,r;
}T[MAX];
void preParse(int u){
if(u==NIL) return;
cout<<" "<<u;
preParse(T[u].l);
preParse(T[u].r);
}
void inParse(int u){
if(u==NIL)return;
inParse(T[u].l);
cout<<" "<<u;
inParse(T[u].r);
}
void postParse(int u){
if(u==NIL) return;
postParse(T[u].l);
postParse(T[u].r);
cout<<" "<<u;
}
int main(){
int n,k,l,r,root;
cin>>n;
for(int i=0;i<n;i++){
T[i].p=NIL;
}
for(int i=0;i<n;i++){
cin>>k>>l>>r;
T[k].l=l;
T[k].r=r;
if(l!=NIL){T[l].p=k;}
if(r!=NIL){T[r].p=k;}
}
for(int i=0;i<n;i++){
if(T[i].p==NIL){root=i;}
}
cout<<"Preorder"<<endl;
preParse(root);
cout<<endl<<"Inorder"<<endl;
inParse(root);
cout<<endl<<"Postorder"<<endl;
postParse(root);
cout<<endl;
return 0;
}