#include <bits/stdc++.h>
using namespace std;
struct TreeNode{
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int val):left(nullptr),right(nullptr),val(val){};
};
vector<vector<int>>spiral(TreeNode* root){
	vector<vector<int>>ans;
	if(root == nullptr)return ans;
	
	queue<TreeNode*>q;
	q.push(root);
	bool trn = false;
	while(!q.empty()){
		int sz = q.size();
		vector<int>lvl;
		for(int i = 0;i<sz;i++){
			auto u = q.front();
			q.pop();
			lvl.push_back(u->val);
			if(u->left){
				q.push(u->left);
			}
			
			if(u->right){
				q.push(u->right);
			}
		}
		if(trn)reverse(lvl.begin(),lvl.end());
		ans.push_back(lvl);
		trn = !trn;
	}
	return ans;
}
TreeNode* buildTree(){
	int x;cin>>x;
	if(x == -1)return nullptr;
	TreeNode* root = new TreeNode(x);
	
	queue<TreeNode*>q;
	q.push(root);
	
	while(!q.empty()){
		auto u = q.front();
		q.pop();
		
		if(cin>>x && x!=-1){
			u->left=new TreeNode(x);
			q.push(u->left);
		}
		
			if(cin>>x && x!=-1){
			u->right=new TreeNode(x);
			q.push(u->right);
		}
	}
	return root;
}
int main() {
	TreeNode* root = buildTree();
	vector<vector<int>>ans = spiral(root);
	for(auto &x : ans){
		for(auto &y:x){
			cout<<y<<" ";
		}
		cout<<endl;
	}
	return 0;
}