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

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

void solve(int *preorder, int *inorder, int *root_pos, int left, int right,
           int total_num);
int get_left_child(int *inorder, int root_id, int num);

vector<int> result;

int main() {
  int n;
  scanf("%d", &n);
  int *preorder = (int *)malloc((n + 1) * sizeof(int));
  int *inorder = (int *)malloc((n + 1) * sizeof(int));
  for (int i = 0; i < n; ++i) {
    scanf("%d", &preorder[i]);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &inorder[i]);
  }
  int *root = (int *)calloc(1, sizeof(int));
  solve(preorder, inorder, root, 0, n, n);
  for (int i = 0; i < n; ++i) {
    if (i)
      putchar(' ');
    printf("%d", result[i]);
  }

  putchar('\\n');
  return 0;
}
void solve(int *preorder, int *inorder, int *root_pos, int left, int right,
           int total_num) {
  if (left >= right)
    return;
  int root_id = preorder[*root_pos];
  int mid = get_left_child(inorder, root_id, total_num);
  (*root_pos)++;
  solve(preorder, inorder, root_pos, left, mid, total_num);
  solve(preorder, inorder, root_pos, mid + 1, right, total_num);
  result.push_back(root_id);
}

int get_left_child(int *inorder, int root_id, int num) {
  int left_num = -1;
  for (int i = 0; i < num; ++i) {
    if (inorder[i] == root_id) {
      left_num = i;
      break;
    }
  }
  return left_num;
}

Error: -11

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
struct node {
  int parent_id;
  int left_child_id;
  int right_child_id;
};

void create_node(int *preorder, int *inorder, node *b, int root_pos, int num);
int get_left_child(int *inorder, int root_pos, int root_id, int num);
void print_postorder(node *b, int root_id, bool *is_first);

int main() {
  int n;
  scanf("%d", &n);
  node *b = (node *)calloc((n + 1), sizeof(node));
  int *preorder = (int *)malloc((n + 1) * sizeof(int));
  int *inorder = (int *)malloc((n + 1) * sizeof(int));
  int *postorder = (int *)malloc((n + 1) * sizeof(int));
  for (int i = 0; i < n + 1; ++i) {
    b[i].parent_id = -1;
    b[i].left_child_id = -1;
    b[i].right_child_id = -1;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &preorder[i]);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &inorder[i]);
  }
  bool *is_first;
  *is_first = true;
  create_node(preorder, inorder, b, 0, n);
  int root_id = preorder[0];
  print_postorder(b, root_id, is_first);
  putchar('\\n');
  return 0;
}
void print_postorder(node *b, int root_id, bool *is_first) {
  if (b[root_id].left_child_id != -1)
    print_postorder(b, b[root_id].left_child_id, is_first);
  if (b[root_id].right_child_id != -1)
    print_postorder(b, b[root_id].right_child_id, is_first);
  if (*is_first) {
    printf("%d", root_id);
    *is_first = false;
  } else {
    printf(" %d", root_id);
  }
}
void create_node(int *preorder, int *inorder, node *b, int root_pos, int num) {
  int root_id = preorder[root_pos];
  int left_pos = get_left_child(inorder, root_pos, root_id, num);
  if (left_pos == -1)
    return;
  if (root_pos < left_pos) {
    int left_id = preorder[root_pos + 1];
    b[root_id].left_child_id = left_id;
    b[left_id].parent_id = root_id;
    create_node(preorder, inorder, b, root_pos + 1, left_pos);
    if (left_pos + 1 < num) {
      int right_id = preorder[left_pos + 1];
      b[root_id].right_child_id = right_id;
      b[right_id].parent_id = root_id;
      create_node(preorder, inorder, b, left_pos + 1, num);
    }
  } else {
    if (left_pos + 1 >= num)
      return;
    int left_id = inorder[root_pos - 1];
    int right_id = inorder[root_pos + 1];
    b[root_id].right_child_id = right_id;
    b[right_id].parent_id = root_id;
    if (left_id == 0 || b[left_id].right_child_id != -1) {
      create_node(preorder, inorder, b, root_pos + 1, num);
      return;
    }
    b[root_id].left_child_id = left_id;
    b[left_id].parent_id = root_id;
  }
}

int get_left_child(int *inorder, int root_pos, int root_id, int num) {
  int left_num = -1;
  for (int i = 0; i < num; ++i) {
    if (inorder[i] == root_id) {
      left_num = i;
      break;
    }
  }
  return left_num;
}