Rokua P1030 First Order arrangement Label:none

Source: Internet
Author: User

Title Description

Give a binary tree of the middle order and order. In order to find out its ordinal arrangement. (The Contract tree node is expressed in different uppercase letters, length <=8).

Input/output format

Input format:

A string of 2 lines, all uppercase letters, representing the middle order and order of a binary tree.

Output format:

1 lines, indicating the first order of a binary tree.

Input and Output Sample input example # #:
Badcbdca
Sample # # of output:
Abcd
Code
1#include <iostream>2#include <cstring>3#include <algorithm>4#include <cstdio>5 using namespacestd;6 Charmid[ -],aft[ -];7 intN;8 voidDfsintmlintOrnintAlintar) {9     if(ml > Mr | | Al > AR)return;Ten      Oneprintf"%c", Aft[ar]); A      for(intk=ml;k<=mr;k++){ -         if(mid[k]==Aft[ar]) { -DFS (ml,k-1, al,al+k-ml-1); theDFS (k +1, mr,al+k-ml,ar-1); -              Break; -         } -     } + } -  + intMain () { A //freopen ("01.in", "R", stdin); atscanf"%s%s", Mid,aft); -N=strlen (AFT)-1; -Dfs0N0, N); -     return 0; -}

Turn a puzzle.

We have a sequence arrangement (left-middle-right) and a post-order arrangement (left-right-middle), which is ordered by the pre-order (middle-left-right).

The first thing to know is that there is a pre-order (sequence) and the middle order can be ordered (pre-order), but only the pre-order and post-order is not able to obtain the middle sequence, proving withheld.

What are the characteristics of post-sequential traversal? The root node is always the last to be accessed.

What is the characteristic of the sequence traversal? The left and right sides of the root node are just its left and the sub-tree.

Let's take a tree to give an example:

First, the root of this tree is a (the last of the order), output A;

Then find the position of a in the sequence, and find that it has three points, which are the left and right sub-trees;

The middle sequence of the left three points and the order of the first three points ordered as the left subtree to DFS, because the first order is the middle-left-right, so first go to the left;

> [L] passed in the middle sequence is deb, the order is EDB-output b,de is left subtree, the same operation;

>> [L] The incoming sequence is DE, the order is ED-output d,e is the right subtree, the same operation;

>>> [R] The incoming middle sequence is E, the order is e-Output e;

> [R] incoming sequence is FCG, the order is FGC-output c,f is the left subtree, the same operation, G is the right subtree, the same operation;

>> [L] passed in the middle sequence is F, the order is F-output f;

>> [R] Incoming middle sequence is G, the order is G-output g;

This completes the process of seeking the first order traversal. (The case where the L/R subtree is empty is omitted.)

Then we can simply complete the problem through DFS, because it is the first order traversal, so each time the direct output sequence of the last point. There is no need to save it.

In the program I did not determine whether it has a subtree but directly Dfs down (for the diagram convenient). Therefore, at the beginning of the DFS function, determine whether the incoming string is greater than 0.

In addition, before someone used a substring, but it is not necessary, because only access without modification, as long as the function is passed to the beginning of the two string and the end of the subscript.

The code is as simple as it is written.

You can think for yourself about the four parameters that are passed in DFS.

Rokua P1030 First Order arrangement Label:none

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.