zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    Spring中使用存储过程
    使用mysql-connector-java.jar连接MySql时出现:Error while retrieving metadata for procedure columns: java.sql.SQLException: Parameter/Column name pattern can not be NULL or empty.
    Spring的JDBC示例
    使用JDBC连接MySql时出现:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration
    Spring的JDBC框架概述
    数据库中的DDL/DML/DCL解释(转)
    沉默的螺旋--digest
    左边老师长征--性命、生命、使命
    柬埔寨旅游小结
    Spring Security 源码分析(四):Spring Social实现微信社交登录
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338327.html
Copyright © 2011-2022 走看看