alexjercan/bugnet huggingface dataset:
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int MAX = 40;

int l[MAX], r[MAX];

void dfs(int v, vector<int> &res) {
  if (l[v] != -1)
    dfs(l[v], res);
  if (r[v] != -1)
    dfs(r[v], res);
  res.push_back(v);
}

int main() {
  int n;
  cin >> n;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<int> c(n), rc(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
    rc[c[i]] = i;
  }
  fill(l, l + n, -1);
  fill(r, r + n, -1);
  int id = 0;
  set<int> vs;
  for (int i = 1; i < n; i++) {
    vs.insert(rc[p[id]]);
    while (true) {
      auto it = vs.upper_bound(min(rc[p[i]], rc[p[id]]));
      if (it == vs.end() || *it >= max(rc[p[i]], rc[p[id]]))
        break;
      id--;
    }
    if (rc[p[id]] > rc[p[i]]) {
      l[p[id]] = p[i];
    } else {
      r[p[id]] = p[i];
    }
    id = i;
  }
  vector<int> res;
  dfs(p[0], res);
  for (int i = 0; i < n; i++) {
    cout << res[i] + 1 << " \\n"[i + 1 == n];
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int MAX = 40;

int l[MAX], r[MAX];

void dfs(int v, vector<int> &res) {
  if (l[v] != -1)
    dfs(l[v], res);
  if (r[v] != -1)
    dfs(r[v], res);
  res.push_back(v);
}

int main() {
  int n;
  cin >> n;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<int> c(n), rc(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
    rc[c[i]] = i;
  }
  fill(l, l + n, -1);
  fill(r, r + n, -1);
  int id = 0;
  set<int> vs;
  for (int i = 1; i < n; i++) {
    vs.insert(rc[p[id]]);
    while (*vs.upper_bound(min(rc[p[i]], rc[p[id]])) < max(rc[p[i]], rc[p[id]]))
      id--;
    if (rc[p[id]] > rc[p[i]]) {
      l[p[id]] = p[i];
    } else {
      r[p[id]] = p[i];
    }
    id = i;
  }
  vector<int> res;
  dfs(p[0], res);
  for (int i = 0; i < n; i++) {
    cout << res[i] + 1 << " \\n"[i + 1 == n];
  }
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define MAX 100

struct Node {
  int p, r, l;
};

Node T[MAX];
int Pre[MAX], In[MAX], Post[MAX];
int n;

int postorder(int i, int n) {
  if (T[n].l != -1) {
    i = postorder(i, T[n].l);
  }
  if (T[n].r != -1) {
    i = postorder(i, T[n].r);
  }
  Post[i] = n;
  return i + 1;
}

void recurcive_search(int node, int root) {
  for (int i = 0; i < n; i++) {
    if (In[i] == node) {
      if (T[root].l == -1) {
        T[root].l = node;
        T[node].p = root;
        return;
      } else {
        recurcive_search(node, T[root].l);
        return;
      }
    } else if (In[i] == root) {
      if (T[root].r == -1) {
        T[root].r = node;
        T[root].p = root;
        return;
      } else {
        recurcive_search(node, T[root].r);
        return;
      }
    }
  }
}

int reconstruct_tree() {
  int i, root;
  for (i = 0; i < n; i++) {
    if (i == 0)
      root = Pre[0];
    else {
      recurcive_search(Pre[i], root);
    }
  }
  return root;
}

int main() {
  int i, root;
  for (i = 0; i < MAX; i++)
    T[i].p = T[i].l = T[i].r = -1;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &Pre[i]);
  for (i = 0; i < n; i++)
    scanf("%d", &In[i]);

  root = reconstruct_tree();
  postorder(0, root);

  for (i = 0; i < n; i++) {
    if (i == 0)
      printf("%d", Post[i]);
    else
      printf(" %d", Post[i]);
  }
  printf("\\n");

  return 0;
}


Error: -11

Fixed submission:

#include <stdio.h>
#define MAX 100

struct Node {
  int p, r, l;
};

Node T[MAX];
int Pre[MAX], In[MAX], Post[MAX];
int n;

int postorder(int i, int n) {
  if (T[n].l != -1) {
    i = postorder(i, T[n].l);
  }
  if (T[n].r != -1) {
    i = postorder(i, T[n].r);
  }
  Post[i] = n;
  return i + 1;
}

void recurcive_search(int node, int root) {
  for (int i = 0; i < n; i++) {
    if (In[i] == node) {
      if (T[root].l == -1) {
        T[root].l = node;
        T[node].p = root;
        return;
      } else {
        recurcive_search(node, T[root].l);
        return;
      }
    } else {
      if (T[root].r == -1) {
        T[root].r = node;
        T[root].p = root;
        return;
      } else {
        recurcive_search(node, T[root].r);
      }
    }
  }
}

int reconstruct_tree() {
  int i, root;
  for (i = 0; i < n; i++) {
    if (i == 0)
      root = Pre[0];
    else {
      recurcive_search(Pre[i], root);
    }
  }
  return root;
}

int main() {
  int i, root;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &Pre[i]);
  for (i = 0; i < n; i++)
    scanf("%d", &In[i]);
  for (i = 0; i < n; i++)
    T[i].p = T[i].l = T[i].r = -1;

  root = reconstruct_tree();
  postorder(0, root);

  for (i = 0; i < n; i++) {
    if (i == 0)
      printf("%d", Post[i]);
    else
      printf(" %d", Post[i]);
  }
  printf("\\n");

  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

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

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();

  return 0;
}

Error: -11

Fixed submission:

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

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> pre[i];
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();

  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

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

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l == r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();

  return 0;
}

Error: -11

Fixed submission:

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

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l = r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();

  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;
int ret[50];
int map[50];
int count2 = 0;

struct treenode {
  int data;
  treenode *leftchild;
  treenode *rightchild;
  treenode(int v) {
    data = v;
    leftchild = NULL;
    rightchild = NULL;
  }
};

void remap(int inorder[], int n) {
  for (int i = 0; i < n; i++) {
    map[inorder[i]] = i;
  }
}

treenode *bip(int pre[], int n, int offset) {
  if (n == 0)
    return NULL;
  int rootval = pre[0];
  int i = map[rootval] - offset;
  treenode *root = new treenode(rootval);
  root->leftchild = bip(pre + 1, i, offset);
  root->rightchild = bip(pre + i + 1, n - i - 1, offset + i + 1);
  return root;
}

void lastorder(treenode *ptr) {
  if (ptr) {
    lastorder(ptr->leftchild);
    lastorder(ptr->rightchild);
    ret[count2++] = ptr->data;
  }
}

int main() {
  int n;
  cin >> n;
  int pre[50];
  for (int i = 0; i < n; i++) {
    cin >> pre[i];
  }
  int inorder[50];
  for (int i = 0; i < n; i++) {
    cin >> inorder[i];
  }
  remap(inorder, n);
  treenode *root;
  root = bip(pre, n, 0);
  lastorder(root);
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << ret[i];
  }
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;
int ret[30];
int map[30];
int count2 = 0;

struct treenode {
  int data;
  treenode *leftchild;
  treenode *rightchild;
  treenode(int v) {
    data = v;
    leftchild = NULL;
    rightchild = NULL;
  }
};

void remap(int inorder[], int n) {
  for (int i = 0; i < n; i++) {
    map[inorder[i]] = i;
  }
}

treenode *bip(int pre[], int n, int offset) {
  if (n == 0)
    return NULL;
  int rootval = pre[0];
  int i = map[rootval] - offset;
  treenode *root = new treenode(rootval);
  root->leftchild = bip(pre + 1, i, offset);
  root->rightchild = bip(pre + i + 1, n - i - 1, offset + i + 1);
  return root;
}

void lastorder(treenode *ptr) {
  if (ptr) {
    lastorder(ptr->leftchild);
    lastorder(ptr->rightchild);
    ret[count2++] = ptr->data;
  }
}

int main() {
  int n;
  cin >> n;
  int pre[30];
  for (int i = 0; i < n; i++) {
    cin >> pre[i];
  }
  int inorder[30];
  for (int i = 0; i < n; i++) {
    cin >> inorder[i];
  }
  remap(inorder, n);
  treenode *root;
  root = bip(pre, n, 0);
  lastorder(root);
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << ret[i];
  }
  cout << endl;
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int n, m = 0, a[40], b[40], c[41];
void solve(int l, int r) {
  if (l > r)
    return;
  int x = a[m++];
  solve(l, c[x] - 1);
  solve(c[x] + 1, r);
  cout << x << (x == a[0] ? "\\n" : " ");
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i], c[b[i]] = i;
  solve(0, n - 1);
  return 0;
}


Error: -11

Fixed submission:

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

int n, pos;
vector<int> pre, ino, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(ino.begin(), find(ino.begin(), ino.end(), root));
  rec(1, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    ino.push_back(k);
  }

  solve();
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>

void to_post(int *A, int *B, int *C, int h) {
  int x = A[0];
  C[h - 1] = x;
  if (h < 2)
    return;
  int m = 0;
  while (B[m] != x)
    m++;
  if (m > 0)
    to_post(A + 1, B, C, m);
  if (h - m - 1 > 0)
    to_post(A + m + 1, B + m + 1, C + m, h - m - 1);
}

int main() {
  int *A, *B, *C;
  int i, n;
  scanf("%d", &n);

  A = new int[n];
  B = new int[n];
  C = new int[n];
  if (A == NULL || B == NULL || C == NULL)
    exit(EXIT_FAILURE);

  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    C[i] = 0;
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &B[i]);
  }

  to_post(A, B, C, n);

  printf("%d", C[0]);
  for (i = 1; i < n; i++) {
    printf(" %d", C[i]);
  }
  printf("\\n");

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>

void to_post(int *A, int *B, int *C, int h) {
  int x = A[0];
  C[h - 1] = x;
  if (h < 2)
    return;
  int m = 0;
  while (B[m] != x)
    m++;
  if (m > 0)
    to_post(A + 1, B, C, m);
  if (h - m - 1 > 0)
    to_post(A + m + 1, B + m + 1, C + m, h - m - 1);
}

int main() {
  int *A, *B, *C;
  int i, n;
  scanf("%d", &n);

  A = new int[n];
  B = new int[n];
  C = new int[n];
  if (A == NULL || B == NULL || C == NULL)
    exit(EXIT_FAILURE);

  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    C[i] = 0;
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &B[n]);
  }

  to_post(A, B, C, n);

  printf("%d", C[0]);
  for (i = 1; i < n; i++) {
    printf(" %d", C[i]);
  }
  printf("\\n");

  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

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

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();
  return 0;
}


Error: -11

Fixed submission:

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

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(1, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

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

int n;
int pre[40];
int in[40];
vector<int> past;

// pre[id]??????????????????, in[l, r)?????¨?????¨v???????????????
void dfs(int id, int l, int r) {
  if (r - l == 1) {
    past.push_back(pre[id]);
    return;
  }

  int i;
  for (i = 0; i < r - l; i++) {
    if (in[l + i] == pre[id]) {
      break;
    }
  }
  if (i > 0) {
    dfs(id + 1, l, l + i);
  }
  if (l + i + 1 < r) {
    dfs(id + 1 + i, l + i + 1, r);
  }
  past.push_back(pre[id]);
}

int main() {
  int i;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> pre[i];
  for (i = 0; i < n; i++)
    cin >> in[i];
  dfs(0, 0, n);

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

Error: 0

Fixed submission:

// Up, Down?????????????????????????????????????????£?????????
//?????????pre_0?????????in_i = pre_0 (i>=0)??¨??????i????????????,
//???pre_0????????´??????i????????????????????¨???
//??¨?????¨v??????????????????????????????,
//??????????????????dfs????????°?°???????????????§????????????
//??????????????????,
//???????????????????????????????????????1????????????????????????????????°???????????????
#include <iostream>
#include <vector>
using namespace std;

int n;
int pre[40];
int in[40];
vector<int> past;

// pre[id]??????????????????, in[l, r)?????¨?????¨v???????????????
void dfs(int id, int l, int r) {
  if (r - l == 1) {
    past.push_back(pre[id]);
    return;
  }

  int i;
  for (i = 0; i < r - l; i++) {
    if (in[l + i] == pre[id]) {
      break;
    }
  }
  if (i > 0) {
    dfs(id + 1, l, l + i);
  }
  dfs(id + 1 + i, l + i + 1, r);
  past.push_back(pre[id]);
}

int main() {
  int i;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> pre[i];
  for (i = 0; i < n; i++)
    cin >> in[i];
  dfs(0, 0, n);

  for (i = 0; i < past.size(); i++) {
    cout << past[i];
    if (i + 1 < past.size())
      cout << " ";
  }
  cout << endl;
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

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

int j = 0, k = 0;
vector<int> in, pre;

int search(int n) {
  int i = 0;
  while (in[i] != n) {
    i++;
  }
  return i;
}

void rec(int l, int r) {
  int c, m;

  if (l > r)
    return;
  c = pre[j];
  j++;
  if (l == r) {
    if (++k > 1)
      printf(" ");
    printf("%d", in[l]);
    return;
  }
  m = search(c);
  rec(l, m - 1);
  rec(m + 1, r);
  if (++k > 1)
    printf(" ");
  printf("%d", in[m]);
  return;
}

int main() {
  int i, n, temp;

  cin >> n;

  for (i = 0; i <= n - 1; i++) {
    cin >> temp;
    pre.push_back(temp);
  }
  for (i = 0; i <= n - 1; i++) {
    cin >> temp;
    in.push_back(temp);
  }
  rec(0, n - 1);
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

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

int j = 0, k = 0;
vector<int> in, pre;

int search(int n) {
  int i = 0;
  while (in[i] != n) {
    i++;
  }
  return i;
}

void rec(int l, int r) {
  int c, m;

  c = pre[j];
  j++;
  if (l > r)
    return;
  if (l == r) {
    if (++k > 1)
      printf(" ");
    printf("%d", in[l]);
    return;
  }
  m = search(c);
  rec(l, m - 1);
  rec(m + 1, r);
  if (++k > 1)
    printf(" ");
  printf("%d", in[m]);
  return;
}

int main() {
  int i, n, temp;

  cin >> n;

  for (i = 0; i <= n - 1; i++) {
    cin >> temp;
    pre.push_back(temp);
  }
  for (i = 0; i <= n - 1; i++) {
    cin >> temp;
    in.push_back(temp);
  }
  rec(0, n - 1);
  printf("\\n");
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

static vector<int> preorder(100), inorder(100);

static void inputOrder(vector<int> &order, int num) {
  for (int i = 0; i < num; i++) {
    cin >> order[i];
  }
}

static int current = 0, outIndex = 0;

static int next() {
  int c = preorder[current];
  current++;
  return c;
}

static int find(int c) {
  auto it = find(inorder.begin(), inorder.end(), c);
  return distance(inorder.begin(), it);
}

static void reconstruction(int l, int r) {
  if (l >= r) {
    return;
  }
  int c = next();
  int m = find(c);
  reconstruction(l, m);
  reconstruction(m + 1, r);

  if (outIndex != 0) {
    cout << " " << c;
  } else {
    cout << c;
  }
  outIndex++;
}

int main() {
  int n;
  cin >> n;

  inputOrder(preorder, n);
  inputOrder(inorder, n);
  reconstruction(0, n);

  cout << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct Node {
  int id;
  int parent;
  int left;
  int right;

  Node() : id(), parent(-1), left(-1), right(-1) {}
};

static vector<Node> TREE(25);

static void inputNode(Node &node, int id) {
  node.id = id;
  cin >> node.left >> node.right;
}

static vector<int> preorder(25), inorder(25);

static void inputOrder(vector<int> &order, int num) {
  for (int i = 0; i < num; i++) {
    cin >> order[i];
  }
}

static int current = 0, outIndex = 0;

static int next() {
  int c = preorder[current];
  current++;
  return c;
}

static int find(int c) {
  auto it = find(inorder.begin(), inorder.end(), c);
  return distance(inorder.begin(), it);
}

static void reconstruction(int l, int r) {
  if (l >= r) {
    return;
  }
  int c = next();
  int m = find(c);
  reconstruction(l, m);
  reconstruction(m + 1, r);

  if (outIndex != 0) {
    cout << " " << c;
  } else {
    cout << c;
  }
  outIndex++;
}

int main() {
  int n;
  cin >> n;

  inputOrder(preorder, n);
  inputOrder(inorder, n);
  reconstruction(0, n);

  cout << endl;

  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

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

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();

  return 0;
}


Error: -11

Fixed submission:

#include <stdio.h>

using namespace std;

#define MAX 10000
#define NIL -1

struct Node {
  int parent, left, right;
};

Node T[MAX];
int n, D[MAX], H[MAX];

void setDepth(int u, int d) {
  if (u == NIL)
    return;
  D[u] = d;
  setDepth(T[u].left, d + 1);
  setDepth(T[u].right, d + 1);
}

int setHeight(int u) {
  int h1 = 0, h2 = 0;
  if (T[u].left != NIL)
    h1 = setHeight(T[u].left) + 1;
  if (T[u].right != NIL)
    h2 = setHeight(T[u].right) + 1;
  return H[u] = (h1 > h2 ? h1 : h2);
}

// 接点 u の兄弟を返す
int getSibling(int u) {
  if (T[u].parent == NIL)
    return NIL;
  if (T[T[u].parent].left != u && T[T[u].parent].left != NIL)
    return T[T[u].parent].left;
  if (T[T[u].parent].right != u && T[T[u].parent].right != NIL)
    return T[T[u].parent].right;
  return NIL;
}

void print(int u) {
  printf("node %d: ", u);
  printf("parent = %d, ", T[u].parent);
  printf("sibling = %d, ", getSibling(u));
  int deg = 0;
  if (T[u].left != NIL)
    deg++;
  if (T[u].right != NIL)
    deg++;
  printf("degree = %d, ", deg);
  printf("depth = %d, ", D[u]);
  printf("height = %d, ", H[u]);

  if (T[u].parent == NIL) {
    printf("root\\n");
  } else if (T[u].left == NIL && T[u].right == NIL) {
    printf("leaf\\n");
  } else {
    printf("internal node\\n");
  }
}

int main(void) {
  int v, l, r, root = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    T[i].parent = NIL;
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &v, &l, &r);
    T[v].left = l;
    T[v].right = r;
    if (l != NIL)
      T[l].parent = v;
    if (r != NIL)
      T[r].parent = v;
  }

  for (int i = 0; i < n; i++)
    if (T[i].parent == NIL)
      root = i;

  setDepth(root, 0);
  setHeight(root);

  for (int i = 0; i < n; i++)
    print(i);

  return 0;
}