Nine degrees OJ topic 1078: two fork tree traverse __ Data structure

Source: Internet
Author: User
Topic Description:

The definition of the sequence, sequence, and subsequent traversal of a binary tree:
Pre-sequence traversal: For any subtree, first access to follow, and then traverse its left subtree, and finally traverse its right subtree;
Sequence traversal: For any subtree, first traverse its left subtree, then access the root, and finally traverse its right subtree;
Subsequent traversal: For any subtree, first traverse its left subtree, then traverse its right subtree, and finally access the root.
Given the sequence traversal and the sequence traversal of a binary tree, the sequential traversal is obtained (hint: the given sequence traversal and the sequence traversal can uniquely determine the sequential traversal). Input:

Two strings whose length n is less than or equal to 26.
The first behavior is the sequence traversal, and the second behavior is sequential traversal.
The node name in the binary tree is expressed in uppercase letters: A,b,c .... Up to 26 nodes. Output:

There may be multiple sets of input samples, for each group of test samples,
Output line, the string to iterate through. Sample Input:

ABC
BAC
fdxeag
Xdefag
Sample output:
BCA
XEDGAF
Source:2006 Tsinghua University computer Research Life Test real problem FAQ:

Problem solving problems? Discuss the topic please visit: http://t.jobdu.com/thread-7801-1-1.html

#include <cstdio>  
#include <cstring>  
const int maxn=105;  
Char PRE[MAXN],IN[MAXN];  
  
struct node{  
    char data;  
    node* LCH;  
    node* rch;  
};  
  
node* Create (int prel,int prer,int inl,int INR) {  
    if (prel>prer) return NULL;  
    node* root=new node;  
    root->data=pre[prel];  
    int k;  
    for (k=inl;k<=inr;k++) if (In[k]==pre[prel]) break;  
    int numleft=k-inl;  
    Root->lch=create (prel+1,prel+numleft,inl,k-1);  
    Root->rch=create (PREL+NUMLEFT+1,PRER,K+1,INR);  
    return root;  
}  
  
void Postorder (node* root) {  
    if (root==null) return;  
    Postorder (Root->lch);  
    Postorder (root->rch);  
    printf ("%c", Root->data);  
}  
  
int main () {while  
    (scanf ("%s%s", Pre,in) ==2) {  
        int n=strlen (pre);  
        node* root=create (0,n-1,0,n-1);  
        Postorder (root);  
        printf ("\ n");  
    }  
    return 0;  
}  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.