POJ 题目2255Tree Recovery(二叉树)

来源:互联网 发布:华为pon网络管理 编辑:程序博客网 时间:2024/06/10 08:58
Tree Recovery
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11450 Accepted: 7188

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 ABCDEFGBCAD CBAD

Sample Output

ACBFGEDCDAB

Source

Ulm Local 1997
两种方法做的
ac代码
#include<stdio.h>#include<string.h>void fun(char pre[],char in[],int n,char post[]){if(n<=0)return;char *p=strchr(in,pre[0]);fun(pre+1,in,p-in,post);//左子树fun(pre+(p-in)+1,p+1,n-(p-in)-1,post+(p-in));//右子树post[n-1]=pre[0];//根}int main(){char pre[30],in[30],post[30];while(scanf("%s%s",pre,in)!=EOF){int n=strlen(pre);getchar();fun(pre,in,n,post);post[n]='\0';printf("%s\n",post);}}

ac代码
#include<stdio.h>#include<string>#include<string.h>#include<iostream>using namespace std;typedef struct tree{int data;struct tree *rchild;struct tree *lchild;}tree1,*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]);if(p==-1)root=NULL;else{root=new tree;root->data=pre[0];if(p==0)root->lchild=NULL;elsecreat(root->lchild,pre.substr(1,p),in.substr(0,p));if(p==len-1)root->rchild=NULL;elsecreat(root->rchild,pre.substr(p+1),in.substr(p+1));}}}void out(tree2 &root){if(root==NULL)return;out(root->lchild);out(root->rchild);printf("%c",root->data);}int main(){string pre,in;while(cin>>pre>>in){tree2 root;creat(root,pre,in);out(root);printf("\n");}}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 502盖子粘到手上怎么办 口红粘在盖子上怎么办 玫瑰手杖永久错过了怎么办 手指沾到502胶水怎么办 我退款了货到了怎么办 世纪天成账号被盗什么也没绑怎么办 韩国电话卡不想用怎么办2018 汽车没有年检交警抓到怎么办 ios软件未受信任怎么办 淘宝开店被管理了怎么办 微店网络异常025怎么办 商家给买家返款转错了怎么办 淘宝号限制下单怎么办 淘宝退货单号填错了怎么办 淘宝买家申请退款不退货怎么办 不支持7天无理由怎么办 淘宝上不给退货怎么办 网购衣服买小了怎么办 淘宝上全球购买到假货怎么办 京东全球购税费怎么办 代购被海关税了怎么办 网上买猫被骗了怎么办 苏宁账号被冻结怎么办 九州娱乐提款未到账怎么办 法院拍卖后不足的余款怎么办 购车后余款没拿怎么办? 抵押房屋被执行后余款怎么办 苏宁无敌券过期怎么办 被亚马逊自营跟卖怎么办 苏宁易购绑定手机后解绑不了怎么办 手机qq注册号码忘了怎么办 原创头条号被限制推荐了怎么办 为什么打开app有广告怎么办 苹果手机浏览器总是弹出广告怎么办 手机上打开页面出现广告怎么办 电脑下面的任务栏变宽了怎么办 酷派手机总是出现广告怎么办 电脑弹出的热点新闻关闭不了怎么办 京东老是弹广告怎么办 电脑右下角出现无法显示网页怎么办 电脑右下角广告关不了怎么办