首页 > 其他 > 详细

UVa 10701 - Pre, in and post

时间:2014-10-12 02:56:38      阅读:261      评论:0      收藏:0      [点我收藏+]

题目:已知树的前根序,中根序遍历转化成后根序遍历。

分析:递归,DS。根据定义递归求解即可。

              前根序:根,左子树,右子树;

              中根序:左子树,根,右子树;

              每次,找到根、左子树、右子树,然后分别递归左子树,右子树,输出根即可。

说明:当时进入ACM实验室的第一个题目。

#include <iostream>
#include <cstdlib> 
#include <cstdio>

using namespace std;

char Per[55],In[55];

void post(int a, int b, int c, int d)
{
	if (a>b) return;
	int r = c;
	while (In[r] != Per[a]) r ++;
	post(a+1, a+r-c, c, r-1);
	post(a+r-c+1, b, r+1, d);
	printf("%c",Per[a]);
}

int main()
{
	int n,m;
	while (~scanf("%d",&n)) 
	for (int i = 0 ; i < n ; ++ i) {
		scanf("%d%s%s",&m,Per,In);
		post(0,m-1,0,m-1);
		printf("\n");
	}
	return 0;
}

UVa 10701 - Pre, in and post

原文:http://blog.csdn.net/mobius_strip/article/details/40008899

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!