#include <bits/stdc++.h>
using namespace std;

struct TreeNode{
	int data;
	TreeNode* right;
	TreeNode* left;
	TreeNode(int val):left(nullptr),right(nullptr),data(val){};
};

void helper(TreeNode* root,vector<int>&curr,vector<vector<int>>&ans){
	if(!root)return;
	
	curr.push_back(root->data);
	if(!root->left && !root->right){
		ans.push_back(curr);
	}else{
		helper(root->left,curr,ans);
		helper(root->right,curr,ans);
	}
	curr.pop_back();
}
vector<vector<int>>print(TreeNode* root){
	vector<vector<int>>ans;
	if(!root)return ans;
	
	vector<int>curr;
	helper(root,curr,ans);
	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 = print(root);
	
	for(auto x : ans){
		for(int y : x){
			cout<<y<< " ";
		}
		cout<<endl;
	}
	return 0;
}