求二叉樹的先序遍歷
TimeLimit: 1000MS Memory Limit: 65536KB
SubmitStatistic
已知一棵二叉樹的中序遍歷和后序遍歷,求二叉樹的先序遍歷
Input
輸入數據有多組,第一行是一個整數t (t<1000),代表有t組測試數據。每組包括兩個長度小于50的字符串,第一個字符串表示二叉樹的中序遍歷序列,第二個字符串表示二叉樹的后序遍歷序列。
Output
輸出二叉樹的先序遍歷序列
Example Input
2
dbgeafc
dgebfca
lnixu
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****************************************************/
新聞熱點
疑難解答