UVA 11234

/*这道题我完全按照题目的意思先把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;
}


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值