#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
const int N = 105;
struct Node {
char value;
Node* lchild;
Node* rchild;
};
stack<Node*> S;
Node* creatTree() {
char c;
cin >> c;
if(c == '#') return NULL;
Node* root = new Node();
root->value = c;
root->lchild = creatTree();
root->rchild = creatTree();
return root;
}
void preOrder(Node* root) {
Node* p = root;
while(1) {
if(p != NULL) {
cout << p->value << ' ';
S.push(p);
p = p->lchild;
}
else {
if(S.empty()) return;
p = S.top();
S.pop();
p = p->rchild;
}
}
}
int main() {
Node* root = creatTree();
preOrder(root);
cout << endl;
return 0;
}
Input : 1 2 3 # 8 # # # 5 # #
Output : 1 2 3 8 5