#include<iostream>
#include<queue>
using namespace std;
#define MaxTree 10
#define Null -1
typedef int ElementType;
typedef int Tree;
struct TNode {
ElementType Data;
Tree Left;
Tree Right;
}T[MaxTree];
int sum = 0;
Tree BuildTree(struct TNode T[]);
void PrintTree(Tree R);
int main() {
Tree R;
R = BuildTree(T);
PrintTree(R);
return 0;
}
Tree BuildTree(struct TNode T[]) {
int N;
cin >> N;
Tree Root = Null;
if (N) {
int i;
char cl, cr;
int* check = new int[N];
for (i = 0; i < N; i++) check[i] = 0;
for (i = 0; i < N; i++) {
cin >> cl >> cr;
T[i].Data = i;
if ((cl == '-') && (cr == '-')) sum++;
if (cl != '-') {
T[i].Left = cl - '0';
check[T[i].Left] = 1;
}
else T[i].Left = Null;
if (cr != '-') {
T[i].Right = cr - '0';
check[T[i].Right] = 1;
}
else T[i].Right = Null;
}
for (i = 0; i < N; i++)
if (!check[i]) break;
Root = i;
}
return Root;
}
void PrintTree(Tree R) {
if (R == Null) return;
queue<Tree>Q;
Q.push(R);
while (!Q.empty()) {
R = Q.front();
Q.pop();
if (T[R].Left == Null && T[R].Right == Null) {
cout << R;
sum--;
if (sum) cout << " ";
}
if (T[R].Left != Null) Q.push(T[R].Left);
if (T[R].Right != Null) Q.push(T[R].Right);
}
}
12-01
630
