poj2225 Tree Recovery(前序中序转后序)

本文介绍了一种从给定的二叉树前序和中序遍历序列重构二叉树的方法,并提供了两种实现思路:一是直接通过递归求解后序遍历序列;二是先构建二叉树再输出后序遍历。文章通过具体的样例输入输出展示了重构过程。

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

Tree Recovery
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11452 Accepted: 7190

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
/*
方法一:递归找
*/
#include<cstdio>
#include<cstring>
//相当于一直还原根节点,到最后一层根节点即叶子节点 
void Recover(char *pre,char *in,int n,char *post){
//传入参数分别为 根节点 左叶子节点 树的长度 后序的左叶子节点 
	if(n<=0)return;
	char *p=strchr(in,pre[0]);//查找根在中序中的位置
	Recover(pre+1,in,(p-in),post);//递归到左子树 
	Recover(pre+(p-in)+1,p+1,n-(p-in)-1,post+(p-in)); //右子树 p-in 为左子树的长度 
	post[n-1]=pre[0];//还原根节点 
}
void solve(){
	char pre[50],in[50],post[50];
	while(scanf("%s%s",pre,in)!=EOF){
		int n=strlen(pre);
		Recover(pre,in,n,post);
		post[n]='\0';
		puts(post);
	}
}
int main(){
	solve();
return 0;
}
/*
方法二:先建树在再输出
暂时没掌握,写不出来。先贴出来吧
Time:2014-8-27 14-54
*/
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
typedef struct Tree{
	int data;
	struct Tree *lChild;
	struct Tree *rChild;
}*tree2;
void Creat(tree2 &root,string pre,string in){
	int len=pre.size();
	if(len==0)
		root=NULL;
	else{
		int p=in.find(pre[0]);//返回在 s中第一次出现的下标 
		if(p==-1)
			root=NULL;
		else{ 
				root=new Tree;
				root->data=pre[0];
				if(p==0)//碰到头,左孩子设为 空 
					root->lChild=NULL;
				else
					Creat(root->lChild,pre.substr(1,p),in.substr(0,p));//起始位置,终止位置 
				
				if(p==len-1)//碰到尾,右孩子设为 空 
					root->rChild=NULL;
				else
					Creat(root->rChild,pre.substr(p+1),in.substr(p+1)); 
			}
	}
}
void Print(tree2 &root){
	if(root==NULL)
		return;
	Print(root->lChild);
	Print(root->rChild);
	printf("%c",root->data);
}
void solve(){
	string pre,in;
	while(cin>>pre>>in){
		tree2 root;
		Creat(root,pre,in);
		Print(root);
		printf("\n");
	}
}
int main(){
	solve();
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值