For a treeTwo fork TreeT, we can recursively define its first-order traversal, the middle sequence traversal, and the post-order traversal:
1. Ordinal traversal (preorder (t) = root node of T + preorder (Zuozi of T) + preorder (right subtree of T))
2, Middle sequence traversal (inorder (t) = inorder (t Zuozi) + t root node + inorder (right subtree of T))
3. Post-sequential traversal (postorder (t) = Postorder (Zuozi of T) + postorder (right subtree of T) + t root node)
Where the plus sign represents a string join operation. For example, the two-fork tree, the first-order traversal is DBACEGF, and the middle-order traversal is ABCDEFG
(the painting is slightly ugly, do not abandon)
Requirements: Enter the first sequence traversal and the middle sequence traversal of a tree, and output his post-order traversal
Sample input:
DBACEGF ABCDEFG
Bcad Cbad
Sample output:
Acbfged
Cdab
Code:
#include <stdio.h> #include <string.h> #define MAXN 10void Build (int n, char *s1, Char *s2, char *s) {
if (n <= 0) return; int p = STRCHR (s2,s1[0])-S2; Locate the root node in the middle sequence traversal in the location build (p,s1+1,s2,s); Recursive construction of the left sub-tree of the post-sequential traversal build (n-p-1, s1+p+1,s2+p+1,s+p);//construction of the right subtree after the post-traversal s[n-1] = s1[0];} int main () { char s1[maxn],s2[maxn],ans[maxn]; while (scanf ("%s%s", S1,s2) ==2) { int nlen = strlen (S1); Build (Nlen,s1,s2,ans); Ans[nlen] = ' + '; Puts (ans); } return 0;}
Through this example, I believe there is a certain understanding of the traversal of the two-fork tree, I do not know if you have, anyway, I have.
Portal: two fork tree hierarchy traversal
Binary Tree Reconstruction-(sequential traversal, middle sequence traversal, post-order traversal)