#include <iostream>
#include<queue>
using namespace std;
class node {
public:
    int data;
    node* left;
    node* right;
    node(int d) {
        data = d;
        left = NULL;
        right = NULL;
    }
};

node* buildTree(){
    queue<node*>q;
    int d;cin>>d;
    if(d==-1)return NULL;
    node*head=new node(d);
    q.push(head);
    int lc,rc;
    while(!q.empty()){
        node*f=q.front();
        q.pop();
        cin>>lc>>rc;
        if(lc!=-1){
            f->left=new node(lc);
            q.push(f->left);
        }
        if(rc!=-1){
            f->right=new node(rc);
            q.push(f->right);
        }
    }
    return head;
}

void printPreOrder(node* &root){
	if(root==NULL)return;
	cout<<root->data<<" ";
	printPreOrder(root->left);
	printPreOrder(root->right);
}

int main()
{
    node*root=buildTree();
    printPreOrder(root);
}
