Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
4
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

int preind = 0;

struct Result {
    Result() : output1(){};
    int output1[10000];
};

Result glob;

int find(vector<int>& arr, int stin, int endin, int data) {
    for (int i = stin; i <= endin; i++) {
        if (arr[i] == data) {
            return i;
        }
    }
    return -1;
}

void printPost(vector<int>& arr, vector<int>& pre, int ins, int ine) {
    if (ins > ine) {
        return;
    }

    int ind = find(arr, ins, ine, pre[preind++]);
    printPost(arr, pre, ins, ind - 1);
    printPost(arr, pre, ind + 1, ine);
    glob.output1[ind] = arr[ind];
}

Result getPostorder(vector<int>& inorder, vector<int>& preorder) {
    printPost(inorder, preorder, 0, inorder.size() - 1);
    return glob;
}

int main() {
    vector<int> arr = {4, 2, 5, 1, 3, 6};
    vector<int> pre = {1, 2, 4, 5, 3, 6};

    Result postorderResult = getPostorder(arr, pre);

    for (int i = 0; i < arr.size(); i++) {
        if (i > 0) {
            cout << " ";
        }
        cout << postorderResult.output1[i];
    }
    cout << endl;

    return 0;
}