首页 > 学院 > 开发设计 > 正文

sdutacm-求二叉树的先序遍历

2019-11-06 06:48:09
字体:
来源:转载
供稿:网友

求二叉树的先序遍历

TimeLimit: 1000MS Memory Limit: 65536KB

SubmitStatistic

PRoblem Description

 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历

Input

 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50的字符串,第一个字符串表示二叉树的中序遍历序列,第二个字符串表示二叉树的后序遍历序列。 

Output

 输出二叉树的先序遍历序列

Example Input

2

dbgeafc

dgebfca

lnixu

linux

Example Output

abdegcf

xnliu

Hint

 

Author

 GYX

#include<string.h>#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<queue>#include<iostream>using namespace std;typedef struct node{   char data;   struct node*l;   struct node*r;}tree;void huifu(char *zhong,char *hou,int len){  if(len==0)  return ;  tree *p = new tree;  p->data = *(hou+len-1);  int i = 0;  for(;i<len;i++)  if(zhong[i]==*(hou+len-1))  {     break;  }   cout<<p->data;  huifu(zhong,hou,i);  huifu(zhong+i+1,hou+i,len-i-1);  return ;}int main(){   char hou[102],zhong[102];   int o;   cin>>o;   while(o--)   {    int i,len;   scanf("%s%s",zhong,hou);   len = strlen(hou);   huifu(zhong,hou,len);   cout<<endl;   }}/***************************************************User name: jk160505徐红博Result: AcceptedTake time: 0msTake Memory: 156KBSubmit time: 2017-02-07 15:20:58****************************************************/


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表