POJ2255—Tree Recovery

本文介绍了一个利用前序和中序遍历重建二叉树的问题,并提供了详细的解题思路和代码实现。通过解析给定的字符串对,读者可以理解如何仅通过两组字符串来重构原始的二叉树结构,特别强调了后序遍历的特征及其在重建过程中的作用。

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

题目描述:

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

转载:優YoU http://user.qzone.qq.com/289065406/blog/1299063032

提示:二叉树遍历而已。。。给出前序和中序,求后序

解题思路

1、前序遍历的第一个字母必是 根

2、在中序遍历的字母串中找出 根字母,那么根字母左右两边的字符串就分别是它的左、右子树

3、利用递归复原二叉树(把子树看作新的二叉树)

4、后序遍历特征:后序遍历字母串 自右至左 依次为:

最外层(总树,设为第0层)右子树的根,内1层右子树的根,内2层右子树的根….内n层右子树的根,内n层左子树的根,内n-1层左子树的根……内1层左子树的根,最外层(总树,第0层)左子树的根。把总树的左子树作为新的总树,继续递归即可。   (注意:总树的叶就是作为“单叶”这棵树本身的右根)

5、输出后序遍历时,只需按4的顺序从左到右排列,再倒置输出即可

代码:

//Memory Time 
//180K    0MS 

#include<iostream>
#include<cstring>
using namespace std;

char post[26];
int point=0;

void right_to_left(char preo[],char inor[])
{
	post[point++]=preo[0];       //根

	const int length=strlen(inor);
	if(length==1)
		return;
	int j=0;
	for(;j<length;j++)
		if(inor[j]==preo[0])
			break;
	const int flag=j;
	int i=++j;
	char preo_temp[26],inor_temp[26];
	bool tag=false;

	for(j=0;i<length;++i,++j)          //提取右子树中序
	{
		inor_temp[j]=inor[i];
		tag=true;
	}

	if(tag)
	{
		inor_temp[j]='\0';
	    for(i=0,j=length-j;j<length;++i,++j)       //提取右子树前序
	        preo_temp[i]=preo[j];
	    preo_temp[i]='\0';

        right_to_left(preo_temp,inor_temp);
	}
		                                            //↑↑↑↑↑
	                                                //处理右子树
//===============================================================
	                                                //处理左子树
	                                                //↓↓↓↓↓
	tag=false;
	for(i=0;i<flag;i++)          //提取左子树中序
	{
		inor_temp[i]=inor[i];
	    tag=true;
	}

	if(tag)
	{
		inor_temp[i]='\0';
		for(i=0,j=1;i<flag;++i,++j)      //提取左子树前序
	    	preo_temp[i]=preo[j];
    	preo_temp[i]='\0';
		
		right_to_left(preo_temp,inor_temp);
	}
	return;
}

int main(void)
{
	char preo[26],inor[26];
	
	while(cin>>preo>>inor)
	{
		right_to_left(preo,inor);

    	for(--point;point>=0;point--)
    		cout<<post[point];
	    cout<<endl;

		point=0;
	}
	return 0;
}

以上都为转载内容~~~~~~~

鄙人的编码能力太有限了,虽然前3步想法相同,但是对于后序遍历的特征认识不够深刻啊,而且对于递归时怎么处理得到的后序遍历完全木有想法了,纠结了很久都不能输出想要的答案,果然还要很努力才行~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值