• Reconstruction of a Tree Aizu


    Write a program which reads two sequences of nodes obtained by the preorder tree walk and the inorder tree walk on a binary tree respectively, and prints a sequence of the nodes obtained by the postorder tree walk on the binary tree.

    Input

    In the first line, an integer n, which is the number of nodes in the binary tree, is given.
    In the second line, the sequence of node IDs obtained by the preorder tree walk is given separated by space characters.
    In the second line, the sequence of node IDs obtained by the inorder tree walk is given separated by space characters.

    Every node has a unique ID from 1 to n. Note that the root does not always correspond to 1.

    Output

    Print the sequence of node IDs obtained by the postorder tree walk in a line. Put a single space character between adjacent IDs.

    Constraints

    1≤n≤40

    Sample Input 1

    5
    1 2 3 4 5
    3 2 4 1 5

    Sample Output 1

    3 4 2 5 1

    Sample Input 2

    4
    1 2 3 4
    1 2 3 4

    Sample Output 2

    4 3 2 1

    Code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00                 11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                  00.                                     ^^0.^
                                  ^ 0                                     ^^110.^
                              0   0 ^                                     ^^^10.01
                       ^^     10  1 1                                      ^^^1110.1
                       01     10  1.1                                      ^^^1111110
                       010    01  ^^                                        ^^^1111^1.^           ^^^
                       10  10^ 0^ 1                                            ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    // Virtual_Judge —— Reconstruction of a Tree Aizu - ALDS1_7_d.cpp created by VB_KoKing on 2019-05-09:10.
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    
     Functions required by the program:
     
     Determination algorithm:
     
     Determining data structure:
     
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first gentle breeze that passed through my ear is you,
    The first star I see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int pos = 0;
    vector<int> pre, in, post;
    
    void rec(int left, int right) {
        if (left >= right) return;
        int root = pre[pos++];
        int m = distance(in.begin(), find(in.begin(), in.end(), root));
        rec(left, m);
        rec(m + 1, right);
        post.push_back(root);
    }
    
    void solve() {
        rec(0, pre.size());
        for (int i = 0; i < post.size(); i++) {
            if (i) cout << ' ';
            cout << post[i];
        }
        cout << endl;
    }
    
    int main() {
        int n;
        cin >> n;
        for (int i = 0; i < n; i++) {
            int k;
            cin >> k;
            pre.push_back(k);
        }
        for (int i = 0; i < n; i++) {
            int k;
            cin >> k;
            in.push_back(k);
        }
        solve();
        return 0;
    }
    
  • 相关阅读:
    Retrofit/Okhttp API接口加固技术实践(上)
    浅析C#中的结构体和类
    iOS中 支付宝钱包具体解释/第三方支付 韩俊强的博客
    Java并发之volatile二
    dynamic initializer和全局变量
    二叉树转换成森林&amp;森林变成二叉树
    这才是真正的裸眼3D!超级震撼!!
    每一个开发人员都应该有一款自己的App
    Hibernate HQL的使用
    我的Android进阶之旅------&gt;Android 关于arm64-v8a、armeabi-v7a、armeabi、x86下的so文件兼容问题
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338326.html
Copyright © 2020-2023  润新知