We know the first and middle order traversal of a tree, and find the back order traversal of the tree ,,,
For example:
Dbacepidermal abcdefg
Acbfged
ACCode:
# Include <stdio. h> # include <string. h> void build (int n, char * S1, char * S2) // construct the post-order traversal process {If (n <= 0) return; int P = strchr (S2, s1 [0])-S2; build (p, S1 + 1, S2); // access left subtree build (n-p-1, S1 + p + 1, s2 + p + 1); // access the right subtree printf ("% C", S1 [0]);} int main () {char a [27], B [27]; while (scanf ("% S % s", a, B) = 2) {int n = strlen (a); Build (n,, b); printf ("\ n");} return 0 ;}
Method 2:
# Include <iostream> # include <string> # include <algorithm> using namespace STD; typedef struct STR {char date; struct Str * l, * r;} * tire, T; tire build (string S, string S1) {tire u = NULL; If (S. size ()> 0) {u = new t; U-> date = s [0]; int K = s1.find (s [0]); u-> L = build (S. substr (1, k), s1.substr (0, k); U-> r = build (S. substr (k + 1), s1.substr (k + 1);} return U;} void delet (tire root) {If (root-> L) delet (root-> L); If (root-> r) delet (root-> r); Delete root;} void postorder (tire root) {If (root-> L) postorder (root-> L); If (root-> r) postorder (root-> r); cout <root-> date ;} int main () {string a, B; while (CIN> A> B) {tire root = build (a, B); postorder (Root ); cout <Endl; delet (Root);} return 0 ;}