国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

sdutacm-求二叉樹的層次遍歷

2019-11-06 06:48:03
字體:
供稿:網(wǎng)友

求二叉樹的層次遍歷

TimeLimit: 1000MS Memory Limit: 65536KB

SubmitStatistic

PRoblem Description

已知一顆二叉樹的前序遍歷和中序遍歷,求二叉樹的層次遍歷。

Input

輸入數(shù)據(jù)有多組,輸入T,代表有T組測試數(shù)據(jù)。每組數(shù)據(jù)有兩個長度小于50的字符串,第一個字符串為前序遍歷,第二個為中序遍歷。

Output

每組輸出這顆二叉樹的層次遍歷。

Example Input

2

abc

bac

abdec

dbeac

Example Output

abc

abcde

Hint

 

Author

fmh

#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 ;}tree* huifu2(char *xian,char *zhong,int len){   tree*head =new tree;   if(len==0)   return NULL;   head->data = *(xian);   int i = 0;   for(;i<len;i++)   {       if(zhong[i]==*xian)       break;   }   head->l = huifu2(xian+1,zhong,i);   head->r = huifu2(xian+i+1,zhong+i+1,len-i-1);   return head;}void ccout(tree*root){    queue<tree*>q;    tree*p =NULL;    if(root)    {      q.push(root);    }    while(!q.empty())    {        p = q.front();        q.pop();        cout<<p->data;        if(p->l)        {           q.push(p->l);        }        if(p->r)        {           q.push(p->r);        }    }}int main(){   char hou[102],zhong[102],xian[102];   int o;   cin>>o;   while(o--)   {    int len;   scanf("%s%s",xian,zhong);   len = strlen(zhong);   tree* root;   root = huifu2(xian,zhong,len);   ccout(root);   cout<<endl;   }}/***************************************************User name: jk160505徐紅博Result: AcceptedTake time: 0msTake Memory: 164KBSubmit time: 2017-02-07 15:42:24****************************************************/


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜兴市| 盐山县| 常熟市| 宁安市| 乌拉特中旗| 永川市| 晋中市| 遂溪县| 临潭县| 教育| 淮南市| 广汉市| 错那县| 蓝田县| 东平县| 平谷区| 尖扎县| 兰考县| 甘孜县| 太仓市| 栖霞市| 越西县| 札达县| 金湖县| 信阳市| 卢龙县| 五大连池市| 广南县| 扶风县| 从江县| 福清市| 三明市| 镇宁| 乌兰县| 台山市| 旅游| 宝山区| 赣榆县| 松阳县| 磐安县| 旅游|