///////////////////////////////////////////////////////////////////////////////////////////////////////
tt2767
Disclaimer: This document is subject to the following agreement free reprint-non-commercial-non-derivative-retention Attribution | Creative Commons by-nc-nd 3.0
To view the update and discussion of this article, please click here: http://blog.csdn.net/tt2767
Link is deleted please Baidu: CSDN tt2767
///////////////////////////////////////////////////////////////////////////////////////////////////////
Written in detail in the comments:
#include <cstdio>#include <iostream>#include <cstring>#include <stack>using namespace STD;Const intN =100000+Ten;CharPre_order[n],in_order[n]; Stack<char>After_order;intK =0;voidBuildintL1,intR1,intL2,intR2);intMain () { while(scanf("%s%s", pre_order,in_order) = =2) {intL =strlen(Pre_order); K =0; Build0, L-1,0, L-1);//Set up a post-calendar while(!after_order.empty ())//Stack LIFO, order just for the post-calendar times{printf("%c", After_order.top ()); After_order.pop (); }puts(""); }return 0;}voidBuildintL1,intR1,intL2,intR2)//Yuxian sequence directly in order to find out the order{if((L1 > R1) | | (L2 > R2))return;Charroot = Pre_order[l1]; After_order.push (root);//Pressing the root into the stack each time intp = L2;//Every time from the beginning of the place to find roots while(In_order[p]! = root) p++;//Find the location of the root in the middle order calendar intCNT = P-L2;//Number of ZuoziBuild (l1+cnt+1, r1,p+1, r2);//Build Right sub-tree, rear-built left subtreeBuild (l1+1, l1+cnt,l2,p-1);}
Copyright NOTICE: This article for Bo Master original article, allow non-commercial reprint, reprint must famous author (CSDN tt2767) and this blog link: http://blog.csdn.net/tt2767.
Reconstruction of the uva536_ tree (followed by the first order, the preface, and the following calendar times)