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;
}