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

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
int n;
vector<int> pre;
vector<int> in;
vector<int> post;
vector<int> child[42];
int tree[42]; // store parent
void postorder(int i, vector<int> child[]) {
  if (i == -1)
    return;
  if (child[i].size() == 0) {
    post.push_back(i);
    return;
  }
  vector<int> temp = child[i];
  postorder(temp.at(0), child);
  postorder(temp.at(1), child);
  post.push_back(i);
}
void inorder(int i, vector<int> child[]) {
  if (i == -1)
    return;
  // if(child[i]==NULL) return;
  vector<int> temp = child[i];
  inorder(temp.at(0), child);
  in.push_back(i);
  inorder(temp.at(1), child);
}
void preorder(int i, vector<int> child[]) {
  if (i == -1)
    return;
  pre.push_back(i);
  vector<int> temp = child[i];
  preorder(temp.at(0), child);
  preorder(temp.at(1), child);
}
int reconstruct(int pre[], int in[], int lp, int rp, int li, int ri) {
  // printf("%d %d %d %d\\n",lp,rp,li,ri);
  if (li == ri) {
    lp++;
    return in[li];
  }
  if (li > ri)
    return -1;
  // printf("%d ",lp);
  int p = pre[lp++];
  int index = 0;
  for (int i = li; i <= ri; i++) {
    if (in[i] == p) {
      index = i;
      break;
    }
  }

  for (int i = li; i <= ri; i++) {
    if (i != index)
      tree[in[i]] = p;
  }
  /*for(int i = lp; i<=rp; i++){
          tree[pre[i]] = p;
  }*/

  // inifitivve loop
  child[p].push_back(reconstruct(pre, in, lp, rp, li, index - 1));
  child[p].push_back(
      reconstruct(pre, in, lp + (index - li), rp, index + 1, ri));
  return p;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    tree[i] = -1;
  }
  int pre[n];
  int in[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &pre[i]);
    pre[i] -= 1;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &in[i]);
    in[i] -= 1;
  }

  reconstruct(pre, in, 0, n - 1, 0, n - 1);
  // printf("%d %d\\n",child[4].at(0),child[4].at(1));

  /*for(int i = 0; i<n; i++){
          printf("%d ",tree[i]);
  }printf("\\n");*/
  // printf("%d",child[4].size());
  /*printf("Preorder\\n");
  preorder(root,child);
  for(int i = 0; i<pre.size(); i++){
          if(i==pre.size()-1)
          printf(" %d\\n",pre.at(i));
          else printf(" %d",pre.at(i));
  }
  printf("Inorder\\n");
  inorder(root,child);
  for(int i = 0; i<in.size(); i++){
          if(i==in.size()-1)
          printf(" %d\\n",in.at(i));
          else printf(" %d",in.at(i));
  }*/
  // printf("Postorder\\n");
  postorder(pre[0], child);
  for (int i = 0; i < post.size(); i++) {
    if (i == post.size() - 1)
      printf("%d\\n", post.at(i) + 1);
    else
      printf("%d ", post.at(i) + 1);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
int n;
vector<int> pre;
vector<int> in;
vector<int> post;
vector<int> child[42];
int tree[40]; // store parent
void postorder(int i, vector<int> child[]) {
  if (i == -1)
    return;
  if (child[i].size() == 0) {
    post.push_back(i);
    return;
  }
  vector<int> temp = child[i];
  postorder(temp.at(0), child);
  postorder(temp.at(1), child);
  post.push_back(i);
}
void inorder(int i, vector<int> child[]) {
  if (i == -1)
    return;
  // if(child[i]==NULL) return;
  vector<int> temp = child[i];
  inorder(temp.at(0), child);
  in.push_back(i);
  inorder(temp.at(1), child);
}
void preorder(int i, vector<int> child[]) {
  if (i == -1)
    return;
  pre.push_back(i);
  vector<int> temp = child[i];
  preorder(temp.at(0), child);
  preorder(temp.at(1), child);
}
int reconstruct(int pre[], int in[], int lp, int rp, int li, int ri) {
  if (li == ri)
    return in[li];
  if (li > ri)
    return -1;

  int p = pre[lp++];
  int index = 0;
  for (int i = li; i <= ri; i++) {
    if (in[i] == p) {
      index = i;
      break;
    }
  }
  for (int i = li; i <= ri; i++) {
    if (i != index)
      tree[in[i]] = p;
  }
  /*for(int i = lp; i<=rp; i++){
          tree[pre[i]] = p;
  }*/

  child[p].push_back(reconstruct(pre, in, lp, rp, li, index - 1));
  child[p].push_back(reconstruct(pre, in, lp, rp, index + 1, ri));
  return p;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    tree[i] = -1;
  }
  int pre[n];
  int in[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &pre[i]);
    pre[i] -= 1;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &in[i]);
    in[i] -= 1;
  }

  reconstruct(pre, in, 0, n - 1, 0, n - 1);
  // printf("%d %d\\n",child[4].at(0),child[4].at(1));

  /*for(int i = 0; i<n; i++){
          printf("%d ",tree[i]);
  }printf("\\n");*/
  // printf("%d",child[4].size());
  /*printf("Preorder\\n");
  preorder(root,child);
  for(int i = 0; i<pre.size(); i++){
          if(i==pre.size()-1)
          printf(" %d\\n",pre.at(i));
          else printf(" %d",pre.at(i));
  }
  printf("Inorder\\n");
  inorder(root,child);
  for(int i = 0; i<in.size(); i++){
          if(i==in.size()-1)
          printf(" %d\\n",in.at(i));
          else printf(" %d",in.at(i));
  }*/
  // printf("Postorder\\n");
  postorder(pre[0], child);
  for (int i = 0; i < post.size(); i++) {
    if (i == post.size() - 1)
      printf("%d\\n", post.at(i) + 1);
    else
      printf("%d ", post.at(i) + 1);
  }

  return 0;
}