#include<stdio.h>
struct node {
    int val;
    int left;
    int right;
};
int main(){
    int n;
    scanf("%d",&n);
    
    if (n < 0){
        printf("Invalid input");
        return 0;
    }
    struct node tree[20];
for(int i=0;i<n;i++){
    scanf("%d %d %d",&tree[i].val,&tree[i].left,&tree[i].right);
}
  int root = n -1;
  int stack1[20],stack2[20];
  int top1= -1,top2= -2;
  stack1[++top1] = root;
  while (top1 >= 0){
      int curr = stack1[top1 --];
      stack2[++top2] = curr;
      if(tree[curr].left != -1)
      stack1[++top1]= tree[curr].left;
      if (tree[curr].right != -1)
      stack1[++top1]=tree[curr].right;
  }
  for (int i =top2;i>=0;i--){
      printf("%d",tree[stack2[i]].val);
      if(i!=0)
      printd(",");
      
  }
  return 0;
}