注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

zjushuiping

追求的只是心中的那一份宁静!

 
 
 

日志

 
 

poj2255  

2010-08-11 16:30:24|  分类: ACM/ICPC |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Tree Recovery
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 4273
Accepted: 2852

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 ABCDEFG
BCAD CBAD

Sample Output

ACBFGED
CDAB

Source

Ulm Local 1997



#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
char pre[26],in[26];

struct Node {
       char data;
       int lchild,rchild;
       }binode[26];
int mount=1;
      


void creat_bitree(int head,int pre_l,int pre_h,int in_l,int in_h)
{
 int i,k;
 for(i=in_l;i<=in_h;i++)
 if(in[i]==pre[pre_l])
 break;
 binode[head].data=in[i];
 if(i==in_l)
 binode[head].lchild=-1;
 else
 {k=mount;mount++;binode[head].lchild=k;creat_bitree(k,pre_l+1,pre_l+i-in_l,in_l,i-1);}
 
 if(i==in_h)
 binode[head].rchild=-1;
 else
 {k=mount;mount++;binode[head].rchild=k;creat_bitree(k,pre_l+i-in_l+1,pre_h,i+1,in_h);}
}
 
int num=0;



void post_order(int head)
{
     if(binode[head].lchild!=-1)
     post_order(binode[head].lchild);
    
    
     if(binode[head].rchild!=-1)
     post_order(binode[head].rchild);
     cout<<binode[head].data;
    
}




int main(int argc, char *argv[])
{
    while(cin>>pre>>in)
    {mount=1;
     num=0;
     int pre_l=0, pre_h=strlen(pre)-1,in_l=0, in_h=strlen(in)-1;
    
    creat_bitree(0,pre_l,pre_h,in_l,in_h);
    post_order(0);
    cout<<endl;
     }
    
     
    system("PAUSE");
    return EXIT_SUCCESS;
}

  评论这张
 
阅读(764)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018