Uva536-tree Recovery

Source: Internet
Author: User

Test instructions

Sequential traversal by pre-sequence traversal and mid-order traversal output

Ideas

The first letter of the pre-sequence traversal is the root node, and the position of the root node is found on the left side of the left dial hand tree, and the right subtree is recursive.

Summarize

From the online search by the pre-sequence traversal and the middle sequence traversal output sequence traversal and by the middle sequence traversal and post-order traversal output pre-sequencing traversal code, PO below

Can not be traversed by the sequence traversal and post-order traversal

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong LongLL;7 Const intMAXN = -;8 CharPRE[MAXN],inch[MAXN];9 voidBuild_posttree (Char*inch,Char*pre,intlen)Ten { One     if(!len)return; A     inti =0; -      for(; i < Len; i++) -         if(inch[i] = = *pre) Break; theBuild_posttree (inch, pre+1, i);// Left -Build_posttree (inch+ i +1, Pre + i +1, Len-i-1);// Right -cout << *Pre; -     return; + } - intMain () + { A     //freopen ("In.txt", "R", stdin); at      while(SCANF ("%s%s", Pre,inch) !=EOF) { -         intLen =strlen (pre); -Build_posttree (inch, Pre, Len); -cout <<Endl; -     } -     return 0; in}

Sequential traversal of the output by the middle sequence traversal and post-order traversal:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong LongLL;7 Const intMAXN = -;8 Char inch[MAXN], POST[MAXN];9 voidBuild_posttree (Char*inch,Char*post,intlen)Ten { One     if(len = =0)return; Acout << * (post + len-1); -     inti =0; -      for(; i < Len; i++) the         if(inch[I] = = * (post + len-1)) Break; -Build_posttree (inch, post, I);// Left -Build_posttree (inch+ i +1, Post + I, len-i-1);// Right -     return ; + } - intMain () + { A    //freopen ("In.txt", "R", stdin); at      while(SCANF ("%s%s",inch, post)! =EOF) { -         intLen =strlen (POST); -Build_posttree (inch, Post, Len); -cout <<Endl; -     } -     return 0; in}

Uva536-tree Recovery

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.