
#include <stdio.h>
#include <stdlib.h>
typedef struct tree
{
struct tree *left;
struct tree *right;
char data;
} Tree;
typedef struct stack
{
int top;
Tree *data[31];
} Stack;
void push(Stack *S, Tree *root)
{
if (S->top < 30)
S->data[++S->top] = root;
return;
}
Tree *pop(Stack *S)
{
if (S->top != -1)
return S->data[S->top--];
else
return NULL;
}
void preorder(Tree *root)
{
if (root)
{
printf("%c ", root->data);
preorder(root->left);
preorder(root->right);
}
}
int main()
{
char a[100];
gets(a);
int i = 0;
Tree *root = NULL;
Stack *S = (Stack *)malloc(sizeof(Stack));
S->top = -1;
while (a[i])
{
if (a[i] >= '0' && a[i] <= '9')
{
root = (Tree *)malloc(sizeof(Tree));
root->left = root->right = NULL;
root->data = a[i];
push(S, root);
}
else if(a[i]!=' ')
{
root = (Tree *)malloc(sizeof(Tree));
root->right = pop(S);
root->left = pop(S);
root->data = a[i];
push(S, root);
}
i++;
}
root = pop(S);
preorder(root);
return 0;
}