Poj 2255 Tree Recovery【dfs遍历树】

本文详细解释了如何从给定的二叉树的先序和中序遍历结果推导出其后续遍历结果,并提供了具体的算法实现。包括递归方法的应用,以及关键步骤的解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tree Recovery
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13448 Accepted: 8393

Description

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. 
This is an example of one of her creations: 
                                               D

                                              / \

                                             /   \

                                            B     E

                                           / \     \

                                          /   \     \

                                         A     C     G

                                                    /

                                                   /

                                                  F


To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG. 
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it). 

Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree. 
However, doing the reconstruction by hand, soon turned out to be tedious. 
So now she asks you to write a program that does the job for her! 

Input

The input will contain one or more test cases. 
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.) 
Input is terminated by end of file. 

Output

For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

Sample Input

DBACEGF ABCDEFG
BCAD CBAD

Sample Output

ACBFGED
CDAB

Source


题意:

给出一个二叉树的先序和中序的遍历结果,求解这棵树后续遍历的结果


题解:

数的前中后三种遍历方式可以用递归实现,进行分解的时候,也是可以用递归实现的

先序遍历的第一个元素是父节点,找到在中序遍历的这个点的位置,将中序序列分成两部分,对非空的子树进行递归


ps:数据结构没学好,到现在只记得大概的做法了,真心做的时候,发现自己代码水平真不行,想到的东西也很难敲出来....


/*
http://blog.youkuaiyun.com/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1005;
char a[maxn],b[maxn];
int x[maxn];//辅助数组
void dfs(int la,int ra,int lb,int rb)
{
	int i=x[a[la]-'A'];//定位节点的位置 
	int j=i-lb;//当前节点的左子树的字符长度 
	int k=rb-i;//当前节点的右子树的字符长度 
	if(j)
	{
		dfs(la+1,la+j,lb,i-1);//递归左子树
	}
	if(k)
	{
		dfs(la+j+1,ra,i+1,rb);//递归右子树
	}
	printf("%c",a[la]);//输出的次序就是后续遍历的结果 
}
int main()
{
	while(~scanf("%s%s",a,b))
	{
		int len=strlen(a);
		for(int i=0;i<len;++i)
		{
			x[b[i]-'A']=i;
			//数组里保存中序的每个字母的下标 
		}
		dfs(0,len-1,0,len-1);
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值