数据结构上机测试4.1:二叉树的遍历与应用1
Time Limit: 1000MS Memory Limit: 65536KBProblem Description
输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。Input
第一行输入二叉树的先序遍历序列;
第二行输入二叉树的中序遍历序列。Output
输出该二叉树的后序遍历序列。Example Input
ABDCEF BDAECFExample Output
DBEFCAHint
Author
#include<stdio.h>#include<string.h>typedef struct tree{ char data; tree * l, *r;}tree;tree * creat(char a[],char b[],int n){ char * q; tree * root; if(n==0) return NULL; else { root=new tree; root->data=a[0]; for(q=b;q!='\0';q++) { if(*q==a[0]) break; } int t; t=q-b; root->l=creat(a+1,b,t); root->r=creat(a+t+1,q+1,n-t-1); } return root;}void lasttravel(tree * p){ if(p) { lasttravel(p->l); lasttravel(p->r); printf("%c",p->data); }}int main(){ int n; char a[100],b[100]; tree * p; while(scanf("%s%s",a,b)!=EOF) { n=strlen(a); p=creat(a,b,n); lasttravel(p); printf("\n"); } return 0;}/***************************************************User name: rchg150633李培培Result: AcceptedTake time: 0msTake Memory: 116KBSubmit time: 2016-10-27 15:11:46****************************************************/
数据结构上机测试4.1:二叉树的遍历与应用1
最新推荐文章于 2023-01-29 22:59:22 发布
