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.levelOrderZZ();
}
private class BinaryTree {
private class Node {
int data;
Node left;
Node right;
}
private Node root;
private int size;
public BinaryTree() {
this.root = this.takeInput(null, false);
}
public Node takeInput(Node parent, boolean ilc) {
int cdata = scn.nextInt();
Node child = new Node();
child.data = cdata;
this.size++;
// left
boolean hlc = scn.nextBoolean();
if (hlc) {
child.left = this.takeInput(child, true);
}
// right
boolean hrc = scn.nextBoolean();
if (hrc) {
child.right = this.takeInput(child, false);
}
// return
return child;
}
public void levelOrderZZ() {
// write your code here
Deque<Node> deque=new ArrayDeque<Node>();
Node root=this.root;
deque.addLast(root);
boolean rev=true;
while(deque.size()!=0){
rev=!rev;
int size=deque.size();
if(rev){
for(int i=0;i<size;i++){
if(deque.peekLast().left!=null){
deque.addFirst(deque.peekLast().right);
}
if(deque.peekLast().right!=null){
deque.addFirst(deque.peekLast().left);
}
System.out.print(deque.pollLast().data+" ");
}
}
else{
for(int i=0;i<size;i++){
if(deque.peekFirst().left!=null){
deque.addLast(deque.peekFirst().left);
}
if(deque.peekFirst().right!=null){
deque.addLast(deque.peekFirst().right);
}
System.out.print(deque.pollFirst().data+" ");
}
}
}
}
}
}