import java.util.*;
public class Main {
static Scanner scn = new Scanner(System.in);
public static void main(String[] args) {
Main m = new Main();
BinaryTree bt = m.new BinaryTree();
bt.topView();
}
private class BinaryTree {
private class Node {
int data;
Node left;
Node right;
}
private Node root;
private int size;
public BinaryTree() {
this.takeInput();
}
public void takeInput() {
LinkedList<Node> queue = new LinkedList<>();
root = new Node();
root.data = scn.nextInt();
queue.addLast(root);
while(!queue.isEmpty()){
Node node = queue.removeFirst();
if(node.data == -1){
continue;
}
node.left = new Node();
node.right = new Node();
node.left.data = scn.nextInt();
node.right.data = scn.nextInt();
queue.addLast(node.left);
queue.addLast(node.right);
}
}
public void topView(){
topView(this.root,false);
}
private void topView(Node node,boolean ilc){
if(node.data == -1){
return;
}
if (node==this.root) {
topView(node.left,true);
System.out.print(node.data+" ");
topView(node.right,false);
return;
}
else {
if (ilc==true) {
topView(node.left,true);
}
else {
topView(node.right,false);
}
}
System.out.print(node.data+" ");
}
}
}