The code is short and simple to implement, and here's the code:
main.cpp//premidgetpost////Created by Xin Wang on 4/29/15.//Copyright (c) Xin Wang. All rights reserved.//#include <iostream>//list of binary tree nodes class template <class T>class binarytreenode{Public:bina Rytreenode () {leftchild = rightchild=0;} Binarytreenode (const t& e) {data=e; Leftchild=rightchild = 0; } binarytreenode (const t& e,binarytreenode *l,binarytreenode *r) {data = e; Leftchild =l; Rightchild =r; } T data; Binarytreenode<t> *leftchild,*rightchild;};/ /define an array, respectively, with an array of pre-sequence traversal and middle sequence traversal int pre_order[100];int mid_order[100]; binarytreenode<int> *translate (int pre_l,int pre_r,int mid_l,int mid_r) {if (pre_r-pre_l<0) {//RET Urn 0; } binarytreenode<int> *root;//new A root node root= new binarytreenode<int>; root->data=pre_order[pre_l];//The first node of the pre-sequence traversal is the node std::cout<<root->data<< "root" <<std::endl; if (pre_r==pre_l) {//If the two are equal, it means that there is only one tree node root->leftchild=null; root->rightchild=null; return root; } int index;//finds the location of the middle sequence traversal with the node in for (index = mid_l; index<=mid_r; index++) {if (mid_order[index] = = Pre_ord Er[pre_l]) {break; }} root->leftchild = Translate (pre_l+1, pre_l+ (index-mid_l), mid_l, index-1);//recursive finding the value of the left child with the node Root->rightchild= Translate (pre_l+ (index-mid_l) +1,pre_r, index+1, mid_r);//recursive Find root node right child's value return root; The post-post traversal is printed out void Post_order (binarytreenode<int> *root) {if (root) {Post_order (root->leftchild); Post_order (Root->rightchild); std::cout<< "Value" <<root->data<<std::endl; }}int Main (int argc, const char * argv[]) {int in=0; std::cout<< "Please enter the length of the array:" <<std::endl; std::cin>>in; std::cout<< "Please enter array pre-order" <<std::endl; int zu; int bb=0; int num=0; while (bb<in) {std::cin>>zu; Pre_order[num]=zu; Num++; bb=bb+1; } std::cout<< "Please enter the middle order of the array" <<std::endl; int Zuzhong; int numzhong=0; int aa=0; while (aa<in) {std::cin>>zuzhong; Mid_order[numzhong]=zuzhong; numzhong++; aa=aa+1; } binarytreenode<int> *root = Translate (0, in-1, 0, in-1); std::cout<< "post-Traversal" <<std::endl; Post_order (root); return 0;}
Recursive implementation of sequential traversal of binary tree in data structure with known pre-order