import java.util.*;
public class Main {
Scanner scn = new Scanner(System.in);
public class BinaryTree {
private class Node {
int data;
Node left;
Node right;
}
private Node root;
private int size;
public BinaryTree() {
this.takeInput();
}
public void takeInput() {
int ht=scn.nextInt();
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();
int item1=scn.nextInt();
node.left.data = item1;
int item2=scn.nextInt();
node.right.data = item2;
queue.addLast(node.left);
queue.addLast(node.right);
}
}
private class VOpair {
Node node;
int vl;
VOpair(Node node, int vl) {
this.node = node;
this.vl = vl;
}
}
public void verticalorderdisplay() {
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
Queue<VOpair> q = new LinkedList<>();
VOpair sp = new VOpair(root, 0);
q.add(sp);
while (!q.isEmpty()) {
VOpair rp = q.remove();
if (!map.containsKey(rp.vl)) {
map.put(rp.vl, new ArrayList<>());
}
map.get(rp.vl).add(rp.node.data);
if (rp.node.left != null && rp.node.left.data != -1) {
VOpair np = new VOpair(rp.node.left, rp.vl - 1);
q.add(np);
}
if (rp.node.right != null && rp.node.right.data != -1) {
VOpair np = new VOpair(rp.node.right, rp.vl + 1);
q.add(np);
}
}
ArrayList<Integer> keys = new ArrayList<>(map.keySet());
Collections.sort(keys);
for (int val : keys) {
System.out.println(map.get(val));
}
}
}
public static void main(String[] args) {
Main m = new Main();
BinaryTree bt = m.new BinaryTree();
bt.verticalorderdisplay();
}
}
