This is to give you a binary tree's first and middle order traversal so that you can push and export the latter order traversal.
Very interesting... Of course, the premise for doing this is to first understand how the first and second order traversal of the binary tree are implemented.
I am too lazy to mention = _ =
I directly paste the code, because this is really the basic requirement of the data structure.
You can also write down the code for the forward and backward traversal and export the first traversal.
I also pasted it together.
1 // through the central and first traversal of Binary Trees ---> the latter traverses 2 3 # include <iostream> 4 using namespace STD; 5 6 const int size = 1010; 7 int pre [size]; 8 int in [size]; 9 bool flag; 10 11 void solve (int * Pre, int * In, int Len) 12 {13 int Index = 0; 14 if (LEN> = 1) 15 {16 while (index <Len & * pre! = * (In + index) 17 + + index; 18 solve (pre + 1, In, index); // left subtree 19 solve (pre + index + 1, in + index + 1, len-(index + 1); // right subtree 20 if (FLAG) 21 {22 cout <* pre; 23 flag = false; 24} 25 else26 cout <"" <* pre; 27} 28} 29 30 int main () 31 {32 cin. sync_with_stdio (false); 33 int N; 34 while (CIN> N) 35 {36 flag = true; 37 for (INT I = 0; I <n; I ++) 38 {39 CIN> pre [I]; 40} 41 for (INT I = 0; I <n; I ++) 42 {43 CIN> in [I]; 44} 45 solve (PRE, In, n); 46 cout <Endl; 47} 48 return 0; 49}
View code
1 // use the binary tree's central and backward traversal ---> first traverse 2 # include <iostream> 3 using namespace STD; 4 5 const int size = 1010; 6 int in [size]; 7 int post [size]; 8 bool flag; 9 10 void solve (int * In, int * post, int Len) 11 {12 INT Pos = 0; 13 if (LEN> = 1) 14 {15 while (Pos <Len & * (in + POS )! = * (Post + len-1) 16 {17 + + Pos; 18} 19 if (FLAG) 20 {21 cout <* (in + POS); 22 flag = false; 23} 24 else25 cout <"<* (in + POS); 26 solve (in, post, POS ); // left subtree 27 solve (in + POS + 1, post + POs, len-pos-1); // right subtree 28} 29} 30 31 int main () 32 {33 int N; 34 while (CIN> N) 35 {36 flag = true; 37 for (INT I = 0; I <n; I ++) 38 CIN> in [I]; 39 for (INT I = 0; I <n; I ++) 40 CIN> post [I]; 41 solve (in, post, n); 42 cout <Endl; 43} 44 return 0; 45} 46/* 47 948 4 7 2 1 8 5 9 3 649 7 4 2 8 9 5 6 3 150 51 1 2 4 7 3 5 8 9 652 */
View code
HDU -- 1710 -- links between various traversal of Binary Trees