Problem p02282 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

vector<int> L;

struct Vertex {
  int left;
  int right;
};

void Postorder(vector<Vertex> &T, int root) {
  if (T[root].left != -1)
    Postorder(T, T[root].left);
  if (T[root].right != -1)
    Postorder(T, T[root].right);
  L.push_back(root + 1);
  return;
}

void Reconstruction(vector<Vertex> &T, vector<int> &Pre, vector<int> &In, int p,
                    int q, int s, int t) {
  int root = Pre[p];
  if (p == q) {
    T[root].left = -1;
    T[root].right = -1;
    return;
  }

  vector<bool> check(1000000, false);

  // k???In????????????root?????????????????????
  int k = -1;
  for (int i = s; i < t + 1; i++) {
    check[In[i]] = true;
    if (In[i] == root) {
      k = i;
      break;
    }
  }

  // l???Pre?????????????????´?????¨???????????????????????????????????????
  int l = p;
  while (l < q) {
    l++;
    if (check[Pre[l]] == false) {
      l--;
      break;
    }
    if (l == q)
      break;
  }

  if (s == k) {
    T[root].left = -1;
    T[root].right = Pre[l + 1];

    Reconstruction(T, Pre, In, l + 1, q, k + 1, t);
  } else if (t == k) {
    T[root].left = Pre[p + 1];
    T[root].right = -1;

    Reconstruction(T, Pre, In, p + 1, l, s, k - 1);
  } else {
    T[root].left = Pre[p + 1];
    T[root].right = Pre[l + 1];

    Reconstruction(T, Pre, In, p + 1, l, s, k - 1);
    Reconstruction(T, Pre, In, l + 1, q, k + 1, t);
  }
}

int main() {
  int n, x;
  cin >> n;
  vector<int> Pre(n);
  vector<int> In(n);
  for (int i = 0; i < n; i++) {
    cin >> x;
    Pre[i] = x - 1;
  }

  for (int i = 0; i < n; i++) {
    cin >> x;
    In[i] = x - 1;
  }

  vector<Vertex> T(n);
  Reconstruction(T, Pre, In, 0, n - 1, 0, n - 1);
  Postorder(T, Pre[0]);

  for (int i = 0; i < L.size(); i++) {
    if (i == L.size() - 1) {
      cout << L[i] << endl;
    } else {
      cout << L[i] << " ";
    }
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

vector<int> L;

struct Vertex {
  int left;
  int right;
};

void Postorder(vector<Vertex> &T, int root) {
  if (T[root].left != -1)
    Postorder(T, T[root].left);
  if (T[root].right != -1)
    Postorder(T, T[root].right);
  L.push_back(root + 1);
  return;
}

void Reconstruction(vector<Vertex> &T, vector<int> &Pre, vector<int> &In, int p,
                    int q, int s, int t) {
  int root = Pre[p];
  if (p == q) {
    T[root].left = -1;
    T[root].right = -1;
    return;
  }

  vector<bool> check(1000000, false);

  // k???In????????????root?????????????????????
  int k = -1;
  for (int i = s; i < t + 1; i++) {
    check[In[i]] = true;
    if (In[i] == root) {
      k = i;
      break;
    }
  }

  // l???Pre?????????????????´?????¨???????????????????????????????????????
  int l = p;
  while (l < q) {
    l++;
    if (l == q)
      break;
    if (check[Pre[l]] = false) {
      l--;
      break;
    }
  }

  if (s == k) {
    T[root].left = -1;
    T[root].right = Pre[l + 1];

    Reconstruction(T, Pre, In, l + 1, q, k + 1, t);
  } else if (t == k) {
    T[root].left = Pre[p + 1];
    T[root].right = -1;

    Reconstruction(T, Pre, In, p + 1, l, s, k - 1);
  } else {
    T[root].left = Pre[p + 1];
    T[root].right = Pre[l + 1];

    Reconstruction(T, Pre, In, p + 1, l, s, k - 1);
    Reconstruction(T, Pre, In, l + 1, q, k + 1, t);
  }
}

int main() {
  int n, x;
  cin >> n;
  vector<int> Pre(n);
  vector<int> In(n);
  for (int i = 0; i < n; i++) {
    cin >> x;
    Pre[i] = x - 1;
  }

  for (int i = 0; i < n; i++) {
    cin >> x;
    In[i] = x - 1;
  }

  vector<Vertex> T(n);
  Reconstruction(T, Pre, In, 0, n - 1, 0, n - 1);
  Postorder(T, Pre[0]);

  for (int i = 0; i < L.size(); i++) {
    if (i == L.size() - 1) {
      cout << L[i] << endl;
    } else {
      cout << L[i] << " ";
    }
  }
  return 0;
}