#include <iostream>
#include <queue>
using namespace std;
#define TRUE 1
#define FALSE 0
#define Elemtype char
typedef struct tNode{
Elemtype data;
struct tNode *leftchild, *rightchild;
}tNode, *biTree;
void initBiTree(biTree &T) {
T = NULL;
}
void createBiTree(biTree &T) {
Elemtype e;
cin >> e;
if(e != '#') {
T = new tNode;
T->data = e;
createBiTree(T->leftchild);
createBiTree(T->rightchild);
}
else {
T = NULL;
}
}
void destroyBiTree(biTree &T) {
if(T == NULL) return ;
else {
destroyBiTree(T->leftchild);
destroyBiTree(T->rightchild);
delete(T);
}
}
bool getLeftChildBiTree(biTree T, biTree &leftChild) {
if(T == NULL) return false;
else {
initBiTree(leftChild);
leftChild = T->leftchild;
return true;
}
}
bool getRightChildBiTree(biTree T, biTree &rightChild) {
if(T == NULL) return false;
else {
initBiTree(rightChild);
rightChild = T->rightchild;
return true;
}
}
void pre_outputBiTree(biTree T) {
if(T == NULL) return ;
else {
cout << T->data << " ";
pre_outputBiTree(T->leftchild);
pre_outputBiTree(T->rightchild);
}
}
void med_outputBiTree(biTree T) {
if(T == NULL) return ;
else {
med_outputBiTree(T->leftchild);
cout << T->data << " ";
med_outputBiTree(T->rightchild);
}
}
void lat_outputBiTree(biTree T) {
if(T == NULL) return ;
else {
lat_outputBiTree(T->leftchild);
lat_outputBiTree(T->rightchild);
cout << T->data << " ";
}
}
void seq_outputBiTree(biTree T) {
queue<biTree> queue;
tNode *p;
queue.push(T);
while(! queue.empty()) {
cout << queue.front()->data << " ";
if(queue.front()->leftchild != NULL)
queue.push(queue.front()->leftchild);
if(queue.front()->rightchild != NULL)
queue.push(queue.front()->rightchild);
p = queue.front();
queue.pop();
}
}
int getDepthBiTree(biTree T) {
if(T == NULL) return 0;
else {
int leftDepth = getDepthBiTree(T->leftchild) + 1;
int rightDepth = getDepthBiTree(T->rightchild) + 1;
return max(rightDepth, leftDepth);
}
}
int main() {
biTree T;
initBiTree(T);
createBiTree(T);
pre_outputBiTree(T);
cout << endl;
med_outputBiTree(T);
cout << endl;
lat_outputBiTree(T);
cout << endl;
seq_outputBiTree(T);
cout << endl;
int res = getDepthBiTree(T);
cout << res << endl;
return 0;
}