/*这道题我完全按照题目的意思先把simple input 读入栈,然后建立符号判断树,然后再用队列也就是树的BFS搜索,思路明确,给的数据也很简单,一遍AC*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 12000
typedef struct node
{
char val;
struct node* left;
struct node* right;
}Node;
Node * stack[MAX];
Node * q[MAX];
char ans[MAX];
int n;
void bfs( Node* root )
{
int front = 0, rear = 1;
Node * q[MAX];
q[0] = root;
while( front < rear )
{
Node * u = q[front++];
ans[n++] = u->val;
if( u->left != NULL ) q[rear++] = u->left;
if( u->right != NULL ) q[rear++] = u->right;
}
return ;
}
Node* CreatTreenull( char a )
{
Node * u = (Node*)malloc(sizeof(Node) );
if( u != NULL )
{
u->val = a;
u->left = u->right = NULL;
}
return u;
}
Node* CreatTree( char a, Node * pr, Node *pl )
{
Node *u = (Node*)malloc( sizeof(Node) );
if( u != NULL )
{
u->val = a;
u->left = pl;
u->right = pr;
}
return u;
}
int main()
{
char str[MAX];
int N, i, top;
Node *prtree;
Node *pltree;
Node *pf;
Node *ptree;
scanf( "%d", &N );
getchar();
while( N-- )
{
gets( str );
top = 0;
for( i = 0; i < strlen(str); i++ )
{
if( islower(str[i]) )
{
Node * ptree = CreatTreenull( str[i] );
stack[top++] = ptree;
}
else
{
prtree = stack[--top];
pltree = stack[--top];
pf = CreatTree(str[i], prtree, pltree );
stack[top++] = pf;
}
}
n = 0;
bfs( pf );
for( i = strlen(str)-1; i >= 0; i-- )
printf( "%c", ans[i] );
printf( "\n" );
}
return 0;
}
UVA 11234
最新推荐文章于 2019-07-31 10:03:13 发布