#include <iostream>
using namespace std;
int _root_, father[1001];
struct
node
{
int n;
int lf, rt;
node()
{
n = lf = rt = 0;
}
}tree[1001];
void
__readandinit__();
int
__findroot__();
void
visitB(node x);
void
visitM(node x);
void
visitP(node x);
int
main()
{
__readandinit__();
visitP(tree[_root_]); cout << endl;
visitM(tree[_root_]); cout << endl;
visitB(tree[_root_]);
return 0;
}
void
__readandinit__()
{
memset(father, 0, sizeof father);
int HowManyNodes, p;
cin >> HowManyNodes;
for(int i = 1; i <= HowManyNodes; i++)
{
cin >> p >> tree[p].lf >> tree[p].rt;
tree[p].n = p;
father[tree[p].lf] = father[tree[p].rt] = p;
}
_root_ = __findroot__();
}
int
__findroot__()
{
int i = 1;
while(father[i] != 0)
i = father[i];
return i;
}
void
visitP(node x)
{
cout << x.n << endl;
if(x.lf != 0)
visitP(tree[x.lf]);
if(x.rt != 0)
visitP(tree[x.rt]);
}
void
visitM(node x)
{
if(x.lf != 0)
visitM(tree[x.lf]);
cout << x.n << endl;
if(x.rt != 0)
visitM(tree[x.rt]);
}
void
visitB(node x)
{
if(x.lf != 0)
visitB(tree[x.lf]);
if(x.rt != 0)
visitB(tree[x.rt]);
cout << x.n << endl;
}