Test instructions
Sequential traversal by pre-sequence traversal and mid-order traversal output
Ideas
The first letter of the pre-sequence traversal is the root node, and the position of the root node is found on the left side of the left dial hand tree, and the right subtree is recursive.
Summarize
From the online search by the pre-sequence traversal and the middle sequence traversal output sequence traversal and by the middle sequence traversal and post-order traversal output pre-sequencing traversal code, PO below
Can not be traversed by the sequence traversal and post-order traversal
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong LongLL;7 Const intMAXN = -;8 CharPRE[MAXN],inch[MAXN];9 voidBuild_posttree (Char*inch,Char*pre,intlen)Ten { One if(!len)return; A inti =0; - for(; i < Len; i++) - if(inch[i] = = *pre) Break; theBuild_posttree (inch, pre+1, i);// Left -Build_posttree (inch+ i +1, Pre + i +1, Len-i-1);// Right -cout << *Pre; - return; + } - intMain () + { A //freopen ("In.txt", "R", stdin); at while(SCANF ("%s%s", Pre,inch) !=EOF) { - intLen =strlen (pre); -Build_posttree (inch, Pre, Len); -cout <<Endl; - } - return 0; in}
Sequential traversal of the output by the middle sequence traversal and post-order traversal:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong LongLL;7 Const intMAXN = -;8 Char inch[MAXN], POST[MAXN];9 voidBuild_posttree (Char*inch,Char*post,intlen)Ten { One if(len = =0)return; Acout << * (post + len-1); - inti =0; - for(; i < Len; i++) the if(inch[I] = = * (post + len-1)) Break; -Build_posttree (inch, post, I);// Left -Build_posttree (inch+ i +1, Post + I, len-i-1);// Right - return ; + } - intMain () + { A //freopen ("In.txt", "R", stdin); at while(SCANF ("%s%s",inch, post)! =EOF) { - intLen =strlen (POST); -Build_posttree (inch, Post, Len); -cout <<Endl; - } - return 0; in}
Uva536-tree Recovery