Topic 1078: Two cross-tree traversal
time limit:1 seconds
Memory limit:32 MB
-
Title Description:
-
The definition of a binary tree's preamble, middle order, and post-order traversal:
Pre-sequence traversal: On either subtree, first accesses the heel, then traverses its left subtree, and finally traverses its right subtree;
Middle sequence traversal: to any subtree, first traverse its left subtree, then access the root, and finally traverse its right subtree;
Post-post traversal: to any subtree, first traverse its left subtree, then traverse its right subtree, and finally access the root.
Given the pre-sequence traversal and the middle sequence traversal of a binary tree, the post-order traversal is obtained (hint: the sequential traversal can be determined only by the given pre-sequence traversal and the middle sequence traversal).
-
Input:
-
Two strings whose length n is less than or equal to 26.
The first behavior of the pre-sequence traversal, the second behavior in the sequence traversal.
The name of the node in the binary tree is denoted by an uppercase letter: A,b,c .... A maximum of 26 nodes.
-
Output:
-
The input sample may have more than one set, for each test sample,
The output line is a string that is iterated through.
-
Sample input:
-
Abcbacfdxeagxdefag
-
-
Sample output:
-
Bcaxedgaf
#include <iostream>#include<string.h>using namespacestd;structNode//tree node Structure{Node*lchild;//left dial hand treeNode *rchild;//Right sub-tree CharC//node character Information} tree[ -];//static memory allocation arrayintLoc//number of nodes already allocated in the static arrayNode *create ()//Request a node space, and return the pointer to its point{tree[loc].lchild=tree[loc].rchild=null;//initialize left and right son is empty return&Tree[loc++];//returns a pointer, and LOC accumulates}Charstr1[ -],str2[ -];//Save pre-order and middle-order traversal result stringsvoidPostorder (Node *t)//Post-post traversal{ if(T->lchild!=null)//left dial hand tree is not empty, traverse left sub-tree{postorder (T-lchild); } if(T->rchild!=null)//right subtree is not empty, traverse right sub-tree{postorder (T-rchild); } printf ("%c", t->c);//traverse the node to output its character information}/*//By the pre-order and the middle order to restore the tree, and return its root node, the pre-order traversal result is str1[s1]-str1[e1], the middle sequence traversal result is str2[s2]=str2[e2];*/Node*build (intS1,intE1,intS2,intE2) {Node*ret=create ();//request space for root noderet->c=str1[s1];//the node character is the first character of a pre-order traversal intRootidx; for(intI=S2; i<=e2; i++)//find the location of the root node character in the middle sequence traversal { if(str2[i]==STR1[S1]) {Rootidx=i; Break; } } if(ROOTIDX!=S2)//Joz tree is not empty{ret->lchild=build (s1+1, s1+ (ROOTIDX-S2), s2,rootidx-1);//(ROOTIDX-S2) for the length of the Zuozi//recursive return of the original left subtree } if(ROOTIDX!=E2)//Right Sub-tree is not empty{ret->rchild=build (s1+ (ROOTIDX-S2) +1, e1,rootidx+1, E2); //return the original right sub-tree by hand } returnRet//returns the root node pointer}intMain () { while(SCANF ("%s", str1)! =EOF) {scanf ("%s", STR2); Loc=0; intl1=strlen (STR1); intL2=strlen (STR2); Node*t=build (0, l1-1,0, l2-1); Postorder (T); printf ("\ n"); } return 0;}
Topic 1078: Two cross-tree traversal