# 1039. 顺序存储二叉树

//100通过
#include<iostream>
#include<cstring>
using namespace std;

#define VirNode -1
#define MAX_TREE_SIZE 30000
typedef int ElemType;
typedef ElemType sqBitTree[MAX_TREE_SIZE];
int a[MAX_TREE_SIZE];
int b[MAX_TREE_SIZE];

int k = 0;

void PostOeder(sqBitTree bt, int sub);

int main()
{
int n;
int i, j, k;
int left, right;
int m = 0;
int data[MAX_TREE_SIZE][3] = { 0 };
sqBitTree sqTree;

cin >> n;
sqTree[0] = n;
for (i = 1; i < MAX_TREE_SIZE; i++)
{
sqTree[i] = -1;
}
for (i = 0; i < n; i++)
{
cin >> data[i][0] >> data[i][1] >> data[i][2];
if (data[i][0] == 1)
{
sqTree[1] = 1; sqTree[2] = data[i][1]; sqTree[3] = data[i][2];
left = data[i][1];
right = data[i][2];
}
}
i = 2;
j = 2;
while (m < n - 1)
{
for (; j < 2 * i; j++)
{
k = 0;
if (sqTree[j] == -1) continue;
while (data[k][0] != sqTree[j]) k++;
sqTree[2 * j] = data[k][1];
sqTree[2 * j + 1] = data[k][2];
m++;
}
i = 2 * i;
}

PostOeder(sqTree, 1);

for (int i = 1; i <= n; i++)
{
cout << a[i] << ' ';
}
cout << endl;
for (int i = 1; i <= n; i++)
{
cout << b[i] << ' ';
}
return 0;
}

void PostOeder(sqBitTree bt, int sub)
{

if (bt[sub] >= 0)
{
if (bt[sub * 2] != VirNode) PostOeder(bt, sub * 2);
if (bt[sub * 2 + 1] != VirNode) PostOeder(bt, sub * 2 + 1);
a[bt[sub]] = sub;
b[++k] = bt[sub];
}
}