cpp
# include <iostream>
# include <cstdio>
struct Node {
int left;
int right;
};
struct Tree {
int n;
int root;
Node * treeList;
Tree(int _n) :n(_n)
{
bool isRoot[15];
for (int i = 0; i < n; ++i) isRoot[i] = true;
treeList = new Node[n];
for (int i = 0; i < n; ++i)
{
char b, c;
scanf("\n%c %c", &b, &c);
if (b == '-')
treeList[i].left = -1;
else
treeList[i].left = b - '0', isRoot[treeList[i].left] = false;
if (c == '-')
treeList[i].right = -1;
else
treeList[i].right = c - '0', isRoot[treeList[i].right] = false;
}
root = n;
while (--root >= 0 && !isRoot[root]);
}
void ListLeaves()
{
int queue[100], front = 0, rare = 0;
queue[++rare] = root;
int isFirst = true;
while (front != rare)
{
int deq = queue[++front];
if (treeList[deq].left != -1) queue[++rare] = treeList[deq].left;
if (treeList[deq].right != -1) queue[++rare] = treeList[deq].right;
if (treeList[deq].left == -1 && treeList[deq].right == -1)
{
printf("%s%d", isFirst ? "" : " ", deq); isFirst = false;
}
}
}
};
int main(void)
{
int n;
scanf("%d", &n);
Tree t(n);
t.ListLeaves();
return 0;
}