2012-06-02 17:52:11Morris

[UVA] 10701 - Pre, in and post

Problem F - Pre, in and post

Time Limit: 1 second

A common problem in data structures is to determine the traversal of a binary tree. There are three classic ways to do it:

Pre-order: You must visit in sequence the root, left subtree and the right subtree.
In-order: You must visit in sequence the left subtree, the root and the right subtree.
Post-order: You must visit in sequence the left subtree, the right subtree and the root.

See the picture below:

graph.png

The pre, in and post-order traversal are, respectively,  ABCDEF, CBAEDF and CBEFDA. In this problem, you must compute the post-order traversal of a binary tree given its in-order and pre-order traversals.

Input

The input set consists of a positive number C ≤ 2000, that gives the number of test cases and C lines, one for each test case. Each test case starts with a number 1 ≤ N ≤ 52, the number of nodes in this binary tree. After, there will be two strings S1 and S2 that describe the pre-order and in-order traversal of the tree. The nodes of the tree are labeled with different characters in the range a..z and A..Z. The values of N, S1 and S2 are separeted by a blank space.

Output

For each input set, you should output a line containing the post-order transversal for the current tree.

Sample input

3
3 xYz Yxz
3 abc cba
6 ABCDEF CBAEDF

Sample output

Yzx
cba
CBEFDA


這東西很久沒寫, 都快忘光了

#include <stdio.h>
char a[60], b[60];
int idx;
void find(int l, int r) {
    if(l > r)   return;
    int i;
    for(i = l; i <= r; i++) {
        if(b[i] == a[idx])
            break;
    }
    if(i != r+1) {
        idx++;
        find(l, i-1);
        find(i+1, r);
        putchar(b[i]);
    }
}
int main() {
    int t, n;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %s %s", &n, a, b);
        idx = 0;
        find(0, n-1);
        puts("");
    }
    return 0;
}

Harrison21 2024-03-03 21:37:33

Great post. The particular submit has an effect on plenty of important difficulties individuals community. We all can't be uninvolved to be able to these kinds of difficulties. This kind of submit offers guidelines and also principles. Extremely useful and also sensible. AustralianCitizenshipSupport http://citizenshiptests.au/

Harrison21 2024-03-03 18:31:51

I'd personally declare that will this is the a terrific article of an wonderful man or woman, i am just very happy to discover this specific. Natural Electrolyte Drinks http://www.healthkept.com/natural-electrolyte-drinks-for-post-workout-hydration/

jun88 2024-02-20 19:41:27

New web site is looking good. Thanks for the great effort. jun88 http://jun88.luxury/