#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
struct node *lchild,*rchild;
int data;
}treeno,*treenode;
void cretree(treenode &root){
int x;
cin>>x;
if(x){
root=new treeno;
root->data=x;
cretree(root->lchild);
cretree(root->rchild);
}else{
root=NULL;
}
}
void allpath(treenode root,treenode path[10],int lenth){
if(root){
if(!root->lchild&&!root->rchild){
cout<<root->data<<" ";
for(int i=lenth-1;i>=0;i--)
cout<<path[i]->data<<" ";
; cout<<endl;
}else{
path[lenth++]=root;
allpath(root->lchild,path,lenth);
allpath(root->rchild,path,lenth);
}
}
}
int main()
{
treenode root,path[10];
cretree(root);//1 2 3 0 0 4 5 0 6 0 0 7 0 0 8 0 0
allpath(root,path,0);
return 0;
}