#include <iostream> #include <string>using namespace std;struct node{ Node * lchild; Node * RCHILD; char c;}; node* build (string in, String pos) {node* t=null;if (in.size () >0) {t=new Node (); T->c=pos[pos.length () -1];t-> Lchild=null;t->rchild=null;} if (In.size () >1) {int root=0;for (int i=0;i<in.size (); i++) if (in[i]==t->c) {root=i; break;} String pos_left = Pos.substr (0, root), string in_left = In.substr (0,root); T->lchild=build (In_left, pos_left); string Pos_right = Pos.substr (root, Pos.length () -1-root), String in_right = In.substr (root+1, In.size ()-root-1); t->rchild= Build (In_right, pos_right);} return t;} void preorder (Node* t) {cout<<t->c;if (t->lchild) preorder (T->lchild), if (t->rchild) PreOrder (t >rchild);} int main () { string in, POS; cin>>in>>pos; node* t= Build (in, POS); Preorder (t); cout<<endl; return 0;}
Sequence traversal and mid-order traversal to determine pre-order traversal