Problem p02282 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

enum struct Type { ROOT, INTERNAL, LEAF };

struct OwnNode {
  int id;
  int parent;
  int depth;
  int height;
  int sibling;
  Type type;
  int left;
  int right;
};

OwnNode tree[100001];
bool isChild[100001];
const string typeStr[] = {"root", "internal node", "leaf"};

int TrackTree(int parentid, int id, int depth) {
  /*int height = 0;
  tree[id].parent = parentid;
  tree[id].depth = depth;
  for (int i = 0; i != tree[id].children.size(); i++)
  {
          height = max(height, TrackTree(id, tree[id].children[i], depth + 1));
  }
  if (tree[id].children.size() == 2)
  {
          tree[tree[id].children[0]].sibling = tree[tree[id].children[1]].id;
          tree[tree[id].children[1]].sibling = tree[tree[id].children[0]].id;
  }
  tree[id].height = height;
  return height + 1;*/
  return 0;
}

void PreOrder(int id) {
  printf(" %d", id);
  if (tree[id].left != -1) {
    PreOrder(tree[id].left);
  }
  if (tree[id].right != -1) {
    PreOrder(tree[id].right);
  }
}

void InOrder(int id) {
  if (tree[id].left != -1) {
    InOrder(tree[id].left);
  }
  printf(" %d", id);
  if (tree[id].right != -1) {
    InOrder(tree[id].right);
  }
}

void PostOrder(int id, int cnt) {
  if (tree[id].left != -1) {
    PostOrder(tree[id].left, 1);
  }
  if (tree[id].right != -1) {
    PostOrder(tree[id].right, 1);
  }
  if (cnt == 0) {
    printf("%d", id);
  } else {
    printf("%d ", id);
  }
}

int Reconstruct(int a[], int b[], int n) {
  if (n == 1) {
    tree[a[0]].left = -1;
    tree[a[0]].right = -1;
    return a[0];
  }

  int tempi = -1;
  for (int i = 0; i < n; i++) {
    if (b[i] == a[0]) {
      tempi = i;
      break;
    }
  }

  int *a1 = new int[tempi];
  int *a2 = new int[n - tempi - 1];
  int *b1 = new int[tempi];
  int *b2 = new int[n - tempi - 1];

  for (int i = 0; i < tempi; i++) {
    a1[i] = a[i + 1];
    b1[i] = b[i];
  }
  for (int i = 0; i < (n - tempi - 1); i++) {
    a2[i] = a[tempi + 1 + i];
    b2[i] = b[tempi + 1 + i];
  }

  int left, right;
  if (tempi != 0) {
    left = Reconstruct(a1, b1, tempi);
    isChild[left] = true;
    // printf("left:%d\n", left);
  } else {
    left = -1;
  }
  if ((n - tempi - 1) != 0) {
    right = Reconstruct(a2, b2, n - tempi - 1);
    isChild[right] = true;
    // printf("right:%d\n", right);
  } else {
    right = -1;
  }

  tree[b[tempi]].left = left;
  tree[b[tempi]].right = right;

  delete[] a1;
  delete[] a2;
  delete[] b1;
  delete[] b2;

  return b[tempi];
}

int main() {
  int n;
  int a[50], b[50];
  cin >> n;

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

  /*for (int i = 0; i < n; i++)
  {
          int id,left,right;
          scanf("%d %d %d", &id, &left, &right);

          tree[id].id = id;
          tree[id].depth = 0;
          tree[id].sibling = -1;
          tree[id].parent = -1;
          tree[id].left = left;
          tree[id].right = right;

          if (left != -1)
          {
                  isChild[left] = true;
          }
          if (right != -1)
          {
                  isChild[right] = true;
          }
  }*/

  Reconstruct(a, b, n);

  int rootID = -1;
  for (int i = 1; i < n; i++) {
    if (!isChild[i]) {
      rootID = i;
      break;
    }
  }

  /*TrackTree(-1, rootID, 0);

  for (int i = 0; i < n; i++)
  {
          if (tree[i].parent == -1)
          {
                  tree[i].type = Type::ROOT;
          }
          else if (tree[i].children.size() == 0)
          {
                  tree[i].type = Type::LEAF;
          }
          else
          {
                  tree[i].type = Type::INTERNAL;
          }
  }

  for (int i = 0; i < n; i++)
  {
          printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d,
  height = %d, %s\n", tree[i].id, tree[i].parent,
  tree[i].sibling,tree[i].children.size(),tree[i].depth,
  tree[i].height,typeStr[(int)tree[i].type].c_str());
  }*/

  /*printf("Preorder\n");
  PreOrder(rootID);
  printf("\nInorder\n");
  InOrder(rootID);
  printf("\nPostorder\n");*/
  PostOrder(rootID, 0);
  printf("\n");

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

enum struct Type { ROOT, INTERNAL, LEAF };

struct OwnNode {
  int id;
  int parent;
  int depth;
  int height;
  int sibling;
  Type type;
  int left;
  int right;
};

OwnNode tree[100001];
bool isChild[100001];
const string typeStr[] = {"root", "internal node", "leaf"};

int TrackTree(int parentid, int id, int depth) {
  /*int height = 0;
  tree[id].parent = parentid;
  tree[id].depth = depth;
  for (int i = 0; i != tree[id].children.size(); i++)
  {
          height = max(height, TrackTree(id, tree[id].children[i], depth + 1));
  }
  if (tree[id].children.size() == 2)
  {
          tree[tree[id].children[0]].sibling = tree[tree[id].children[1]].id;
          tree[tree[id].children[1]].sibling = tree[tree[id].children[0]].id;
  }
  tree[id].height = height;
  return height + 1;*/
  return 0;
}

void PreOrder(int id) {
  printf(" %d", id);
  if (tree[id].left != -1) {
    PreOrder(tree[id].left);
  }
  if (tree[id].right != -1) {
    PreOrder(tree[id].right);
  }
}

void InOrder(int id) {
  if (tree[id].left != -1) {
    InOrder(tree[id].left);
  }
  printf(" %d", id);
  if (tree[id].right != -1) {
    InOrder(tree[id].right);
  }
}

void PostOrder(int id, int cnt) {
  if (tree[id].left != -1) {
    PostOrder(tree[id].left, 1);
  }
  if (tree[id].right != -1) {
    PostOrder(tree[id].right, 1);
  }
  if (cnt == 0) {
    printf("%d", id);
  } else {
    printf("%d ", id);
  }
}

int Reconstruct(int a[], int b[], int n) {
  if (n == 1) {
    tree[a[0]].left = -1;
    tree[a[0]].right = -1;
    return a[0];
  }

  int tempi = -1;
  for (int i = 0; i < n; i++) {
    if (b[i] == a[0]) {
      tempi = i;
      break;
    }
  }

  int *a1 = new int[tempi];
  int *a2 = new int[n - tempi - 1];
  int *b1 = new int[tempi];
  int *b2 = new int[n - tempi - 1];

  for (int i = 0; i < tempi; i++) {
    a1[i] = a[i + 1];
    b1[i] = b[i];
  }
  for (int i = 0; i < (n - tempi - 1); i++) {
    a2[i] = a[tempi + 1 + i];
    b2[i] = b[tempi + 1 + i];
  }

  int left, right;
  if (tempi != 0) {
    left = Reconstruct(a1, b1, tempi);
    isChild[left] = true;
    // printf("left:%d\n", left);
  } else {
    left = -1;
  }
  if ((n - tempi - 1) != 0) {
    right = Reconstruct(a2, b2, n - tempi - 1);
    isChild[right] = true;
    // printf("right:%d\n", right);
  } else {
    right = -1;
  }

  tree[b[tempi]].left = left;
  tree[b[tempi]].right = right;

  delete[] a1;
  delete[] a2;
  delete[] b1;
  delete[] b2;

  return b[tempi];
}

int main() {
  int n;
  int a[50], b[50];
  cin >> n;

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

  /*for (int i = 0; i < n; i++)
  {
          int id,left,right;
          scanf("%d %d %d", &id, &left, &right);

          tree[id].id = id;
          tree[id].depth = 0;
          tree[id].sibling = -1;
          tree[id].parent = -1;
          tree[id].left = left;
          tree[id].right = right;

          if (left != -1)
          {
                  isChild[left] = true;
          }
          if (right != -1)
          {
                  isChild[right] = true;
          }
  }*/

  Reconstruct(a, b, n);

  int rootID = -1;
  for (int i = 1; i <= n; i++) {
    if (!isChild[i]) {
      rootID = i;
      break;
    }
  }

  /*TrackTree(-1, rootID, 0);

  for (int i = 0; i < n; i++)
  {
          if (tree[i].parent == -1)
          {
                  tree[i].type = Type::ROOT;
          }
          else if (tree[i].children.size() == 0)
          {
                  tree[i].type = Type::LEAF;
          }
          else
          {
                  tree[i].type = Type::INTERNAL;
          }
  }

  for (int i = 0; i < n; i++)
  {
          printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d,
  height = %d, %s\n", tree[i].id, tree[i].parent,
  tree[i].sibling,tree[i].children.size(),tree[i].depth,
  tree[i].height,typeStr[(int)tree[i].type].c_str());
  }*/

  /*printf("Preorder\n");
  PreOrder(rootID);
  printf("\nInorder\n");
  InOrder(rootID);
  printf("\nPostorder\n");*/
  PostOrder(rootID, 0);
  printf("\n");

  return 0;
}