Reconstruction of the uva536_ tree (followed by the first order, the preface, and the following calendar times)

Source: Internet
Author: User

///////////////////////////////////////////////////////////////////////////////////////////////////////
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.