#include<bits/stdc++.h>
typedef struct node
{
char data;
struct node *lchild,*rchild;
}BTNode;
void Greate(BTNode *&T)
{
char ch;
scanf("%c",&ch);
if(ch=='#') T=NULL;
else
{
T=(BTNode*)malloc(sizeof(BTNode));
T->data=ch;
Greate(T->lchild);
Greate(T->rchild);
}
}
void Disp(BTNode *T)
{
if(T!=NULL)
{
printf("%c",T->data);
if(T->lchild!=NULL || T->rchild!=NULL)
{
Disp(T->lchild);
if(T->rchild!=NULL) Disp(T->rchild);
}
}
}
bool ancestor(BTNode *T,char x)
{
if(T==NULL) return false;
else if(T->lchild!=NULL && T->lchild->data==x || T->rchild!=NULL && T->rchild->data==x)
{
printf("%c",T->data);
return true;
}
else if(ancestor(T->lchild,x)||ancestor(T->rchild,x))
{
printf("%c",T->data);
return true;
}
else return false;
}
int main()//ABD#G###CE##F##
{
BTNode *T;
Greate(T);
Disp(T);
printf("\n");
char ch='D';
ancestor(T,ch);
return 0;
}