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

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

int main() {
  int n, id = 1, nv, ch, index, syuryo = 1;

  scanf("%d\\n", &n);

  int *A = new int[100040];
  bool *B = new bool[100040];
  int *C = new int[100040];
  string *D = new string[100040];
  int st, ed;
  for (int i = 0; i <= 100040; i++) {
    A[i] = 0;     // hukasa
    C[i] = -1;    // oya
    B[i] = false; // false nara ko wo motte inai
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &id);

    getline(cin, D[id]);

    syuryo = 1;
    for (int r = 1; r <= D[id].length(); r++) {
      if (D[id][r] == ' ') {
        syuryo = r - 1; // String no iti gime
        break;
      }
    }

    index = atoi(D[id].substr(1, syuryo).c_str());

    if (index != 0)
      B[id] = true;

    if (index != 0) {
      st = syuryo + 2;
      for (int d = syuryo + 2; d <= D[id].length(); d++) {
        if (D[id][d] == ' ' || d == D[id].length()) {
          A[atoi(D[id].substr(st, d - st).c_str())] = A[id] + 1;
          C[atoi(D[id].substr(st, d - st).c_str())] = id;
          if (B[atoi(D[id].substr(st, d - st).c_str())]) {
            int st2 = 3;

            for (int j = syuryo + 2;
                 j <= D[atoi(D[id].substr(st, d - st).c_str())].length(); j++) {

              if (D[atoi(D[id].substr(st, d - st).c_str())][j] == ' ' ||
                  j == D[atoi(D[id].substr(st, d - st).c_str())].length()) {
                A[atoi(D[atoi(D[id].substr(st, d - st).c_str())]
                           .substr(st2, j - st2)
                           .c_str())] =
                    A[atoi(D[id].substr(st, d - st).c_str())] + 1;
                st2 = j + 1;
              }
            }
          }
          st = d + 1;
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    printf("node %d: parent = %d, depth = %d, ", i, C[i], A[i]);
    int cou = 0;
    if (C[i] == -1) {
      printf("root, [");
    } else if (!B[i])
      printf("leaf, [");
    else
      printf("internal node, [");

    syuryo = 1;
    for (int r = 1; r <= D[i].length(); r++) {
      if (D[i][r] == ' ') {
        syuryo = r - 1;
        break;
      }
    }
    index = atoi(D[i].substr(1, syuryo).c_str());

    st = syuryo + 2;
    if (index != 0) {
      for (int j = syuryo + 2; j <= D[i].length(); j++) {
        if (D[i][j] == ' ' || D[i].length() == j) {
          printf("%d", atoi(D[i].substr(st, j - st).c_str()));
          if (j != D[i].length())
            printf(", ");
          st = j + 1;
        }
      }
    }
    printf("]\\n");
  }

  delete[] A;
  delete[] B;
  delete[] C;
  delete[] D;
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int Tsukasa[1000];  // parent
int Hizikata[1000]; // left-child
int Yuuma[1000];    // right-child  May Be Yuma
bool used[1000];
int counter = 0, n, Pos;
bool finfin = false;

void Init(int n) {
  for (int i = 0; i < n; i++) {
    Tsukasa[i] = -1;
    Hizikata[i] = -1;
    Yuuma[i] = -1;
    used[i] = false;
  }
}

int SRoot(int n) { // root search
  for (int i = 0; i < n; i++)
    if (Tsukasa[i] == -1)
      return i;
  return -2;
}

void Print(int s) { // print s and count number of used node
  printf("%d", s);
  counter++;
  if (counter != n)
    printf(" ");
  used[s] = true;
}
void BInit(int n) { // init of array
  for (int i = 0; i < n; i++) {
    used[i] = false;
  }
}

int Pre(int s) { // Preorder
  if (s != -1) {
    Print(s);
    Pre(Hizikata[s]);
    Pre(Yuuma[s]);
  }
}

int In(int s) { // Inorder
  if (s != -1) {
    In(Hizikata[s]);
    Print(s);
    In(Yuuma[s]);
  }
}

int Po(int s) { // Postorder
  if (s != -1) {
    Po(Hizikata[s]);
    Po(Yuuma[s]);
    Print(s);
  }
}

int main() { // most important function. perhaps we cannot take "accepted"
             // without this.
  int id, lef, rig, rn;

  scanf("%d\\n", &n);

  Init(n);
  rn = n;
  while (rn-- > 0) {
    scanf("%d %d %d\\n", &id, &lef, &rig);
    Tsukasa[lef] = Tsukasa[rig] = id;
    Hizikata[id] = lef;
    Yuuma[id] = rig;
  }
  // Tsukasa-array is used to store datas about parent
  // if node doesnt have parent, it is -1

  printf("Preorder\\n");
  counter = 0;
  printf(" ");
  Pre(SRoot(n));
  printf("\\n");
  BInit(n);
  counter = 0;
  printf("Inorder\\n");
  printf(" ");
  In(SRoot(n));
  printf("\\n");
  BInit(n);
  counter = 0;
  printf("Postorder\\n");
  finfin = false;
  BInit(n);
  printf(" ");
  Po(SRoot(n));
  printf("\\n");
  return 0;
}