POj 2255

来源:互联网 发布:微博域名怎么改 编辑:程序博客网 时间:2024/04/29 19:25
Tree Recovery
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 5996
Accepted: 3932

Description

Little Valentineliked playing with binary trees very much. Her favorite game wasconstructing randomly looking binary trees with capital letters inthe 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 twostrings for each tree: a preorder traversal (root, left subtree,right subtree) and an inorder traversal (left subtree, root, rightsubtree). For the tree drawn above the preorder traversal isDBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enoughinformation to reconstruct the tree later (but she never triedit).

Now, years later, looking again at the strings, she realized thatreconstructing the trees was indeed possible, but only because shenever had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to betedious.
So now she asks you to write a program that does the job forher!

Input

The input willcontain one or more test cases.
Each test case consists of one line containing two strings preordand inord, representing the preorder traversal and inordertraversal of a binary tree. Both strings consist of unique capitalletters. (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 thetree's postorder traversal (left subtree, right subtree,root).

Sample Input

DBACEGF ABCDEFGBCAD CBAD

Sample Output

ACBFGEDCDAB

Source

Ulm Local 1997
本来听学长讲的是用递归做的,可是做着做着就变成了非递归程序了,其实原理很简单,就是遇到在str2中的位置大于当前位置则向又,否则向左,直到为空时,赋值
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char str1[30],str2[30];
typedef struct tree{
    intdata;
    struct tree*left,*right;
}node,*Lnode;
int m=sizeof(node);
Lnode newnode()
{
    Lnodep;
   p=(Lnode)malloc(m);
   p->left=NULL;
   p->right=NULL;
    returnp;
}
int find(char s)
{
    int i;
   for(i=0;str2[i];i++)
      if(str2[i]==s)return i;
    return0;
}
void post(Lnode p)
{
   if(p==NULL)return;
   post(p->left);
   post(p->right);
   printf("%c",str2[p->data]);
}
int main()
{
    Lnoderoot,p,s;
    inti,flag;
   while(scanf("%s %s",str1,str2)!=EOF)
    {
      root=newnode();
      root->data=find(str1[0]);
      for(i=1;str1[i];i++)
       {
         p=newnode();
         p=root;
         while(1){
            if(find(str1[i])>p->data)
             {
               if(p->right==NULL)
               {flag=1;   break;}
                elsep=p->right;
             }
             else {
               if(p->left==NULL)
               {flag=2;   break;}
                else
               p=p->left;
             }
          }
         s=newnode();
         s->data=find(str1[i]);
         if(flag==1)p->right=s;
          elsep->left=s;
       }
      post(root);
      printf("\n");
    }
    return0;
}

原创粉丝点击