// Problem 1: Mirroring a Hierarchical Network
#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

// Function to create a new node
TreeNode* newNode(int val) {
    TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
    node->val = val;
    node->left = node->right = NULL;
    return node;
}

// Recursively mirrors the tree
void mirrorTree(TreeNode* root) {
    if (root == NULL) {
        return;
    }
    // Swap the children
    TreeNode* temp = root->left;
    root->left = root->right;
    root->right = temp;
    
    // Recurse for subtrees
    mirrorTree(root->left);
    mirrorTree(root->right);
}

// Performs in-order traversal and prints the values
void inorderTraversal(TreeNode* root) {
    if (root == NULL) {
        return;
    }
    inorderTraversal(root->left);
    printf("%d ", root->val);
    inorderTraversal(root->right);
}

int main() {
    int n;
    scanf("%d", &n);
    if (n <= 0) {
        printf("\n");
        return 0;
    }

    TreeNode* nodes[n];
    int is_child[n + 1] = {0}; // To find the root (1-based index)

    // A map from server ID to its index in the `nodes` array
    int id_to_idx[1001]; // Assuming N <= 1000
    int left_indices[n], right_indices[n];

    for (int i = 0; i < n; i++) {
        int id, left_idx, right_idx;
        scanf("%d %d %d", &id, &left_idx, &right_idx);
        nodes[i] = newNode(id);
        id_to_idx[id] = i;
        left_indices[i] = left_idx;
        right_indices[i] = right_idx;
        if (left_idx != -1) is_child[left_idx] = 1;
        if (right_idx != -1) is_child[right_idx] = 1;
    }

    // Link the nodes
    for (int i = 0; i < n; i++) {
        if (left_indices[i] != -1) {
            nodes[i]->left = nodes[id_to_idx[left_indices[i]]];
        }
        if (right_indices[i] != -1) {
            nodes[i]->right = nodes[id_to_idx[right_indices[i]]];
        }
    }
    
    // Find the root (the node that is never a child)
    TreeNode* root = NULL;
    for (int i = 0; i < n; i++) {
         int is_a_child = 0;
         for(int j=0; j<n; ++j) {
             if(left_indices[j] == nodes[i]->val || right_indices[j] == nodes[i]->val) {
                 is_a_child = 1;
                 break;
             }
         }
         if(!is_a_child) {
             root = nodes[i];
             break;
         }
    }
    
    mirrorTree(root);
    inorderTraversal(root);
    printf("\n");

    return 0;
}