HDU 5444 Elven Postman (sequential traversal based on first order traversal and middle order traversal) ACM/ICPC Asia regional Changchun Online

Source: Internet
Author: User

Very pit of a problem, read a long time to read the problem, rushed to finish (set template + Modify template), and then re to die ...

Test instructions

The surface tells us this is a binary tree, and then told us its first order traversal, and then, no ... No more!

Repeated reading, and finally in the occasional notice of this sentence-"Not only so, when numbering the rooms, they always number the rooms number from the East-most Positi On to the west. "

It tells us that the east side of the point is always smaller than the West Point-that is to say, the tree's middle order traversal is a arithmetic progression from 1 to n ...

Input:

The first line input an integer t, indicating that there is a T group of data;

Then the first row of each group of data is an integer n, indicating that there are N nodes in the tree.

The next line has n integers representing the first sequence traversal of the tree.

The next line is an integer m, which represents M-times query.

The next line has a M integer that represents the point of each query.

Output:

Each time the query outputs the path from the root node to the query node, the path outputs ' E ' every time to the left, outputting ' W ' to the right.

Problem Solving Ideas:

Set up the template, set up this tree, and use a fm[] array to tell the parent node of each node to record it. Then you can use it when you query.

But there are two points to note:

1. Since I am a tree using pointers, remember to free up memory for every set of data you use!

2. Each root node of the second set of data may not be the root node in the first set of data, so remember to set the parent node of the root node Fm[root] to 0. My live quota ah ... So leave me and go t_t,555555 ...

Do not say, said more is the tears ....

On the code--

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <cmath>5#include <algorithm>6 using namespacestd;7 8 structNode9 {Ten     intC; OneNode *Left ; ANode *Right ; - }; -  the intfm[1010]; - intt, N, M; - Chardis[1010]; - intpree[1010],ine[1010]; +  -node* Buildtree (int*pre,int*inch,intLength//Achievements + { A     if(Length = =0)returnNULL; atnode* node = (node*)malloc(sizeof(Node)); -Node->c = pre[0]; -     intRootindex =-1; -      for(inti =0; I < length;i++) -     { -         if(inch[i] = = pre[0]) in         { -Rootindex =i; to              Break; +         } -     } theNode->left = Buildtree (pre+1,inch, Rootindex);// Left *Node->right = Buildtree (pre+1+rootindex,inch+rootindex+1, length-rootindex-1);// Right $     returnnode;Panax Notoginseng } -  the voidPrint (Node *root)//Record parent Node + { A     if(Root! =NULL) the     { +         if(Root->left! = NULL) Fm[root->left->c] = (root->c) *Ten; -Print (root->Left ); $         if(Root->right! = NULL) Fm[root->right->c] = (root->c) *Ten+1; $Print (root->Right ); -     } - } the  - voidDFS (node* root)//Freeing MemoryWuyi { the     if(Root! =NULL) -     { Wu         if(Root->left! = NULL) Dfs (root->Left ); -         if(Root->right! = NULL) Dfs (root->Right ); About          Free(root); $     } - } -  - intMain () A { +     //freopen ("test.in", "R", stdin); thescanf"%d", &t); -      while(t--) $     { thescanf"%d", &n); the          for(inti =0; I < n; i++) the         { thescanf"%d", &pree[i]); -Ine[i] = i+1; in         } theNode *root =buildtree (pree, INE, n); theFM[ROOT-&GT;C] =0;//It's here, it's killing me ... I @#¥%^&*! About print (root); thescanf"%d",&m); the          for(inti =0; I < m; i++) the         { +             intmid; -scanf"%d", &mid); the             intK =0;Bayi              while(Fm[mid]! =0) the             { the                 if(fm[mid]%Ten==1) Dis[k] ='W'; -                 ElseDIS[K] ='E'; -MID = fm[mid]/Ten; thek++; the             } the              for(intj = k1; J >=0; j--) printf ("%c", Dis[j]);//Output Path theprintf"\ n"); -         } the Dfs (root); the     } the     return 0;94}
View Code

HDU 5444 Elven Postman (sequential traversal based on first order traversal and middle order traversal) ACM/ICPC Asia regional Changchun Online

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.