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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define SORT(c) sort((c).begin(), (c).end())
const double EPS = 1e-10;                 // sample:if((double)>=(double)+EPS)
#define CLR(a) memset((&a), 0, sizeof(a)) // clear memory
#define dump(x) cerr << #x << " = " << (x) << endl; // debug
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
// cin,cout release
// std::cin.tie(0);
// ios::sync_with_stdio(false);

using namespace std;
#define NIL -1
#define MAX 100005
struct Node {
  int p, l, r;
};
Node T[MAX];
int n, D[MAX];
void print(int u) {
  cout << "node " << u << ": parent = " << T[u].p << ", "
       << "depth = " << D[u] << ", ";
  if (T[u].p == NIL)
    cout << "root, [";
  else if (T[u].l == NIL)
    cout << "leaf, [";
  else
    cout << "internal node, [";
  for (int i = 0, c = T[u].l; c != NIL; i++, c = T[c].r) {
    if (i)
      cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}
void rec(int u, int p) {
  D[u] = p;
  if (T[u].r != NIL)
    rec(T[u].r, p);
  if (T[u].l != NIL)
    rec(T[u].l, p + 1);
}
int main() {
  cin >> n;
  rep(i, n) T[i].p = T[i].l = T[i].r = NIL;
  int l = 0;
  rep(i, n) {
    int v, d;
    cin >> v >> d;
    rep(j, d) {
      int c;
      cin >> c;
      if (!j)
        T[v].l = c;
      else
        T[l].r = c;
      l = c;
      T[c].p = v;
    }
  }
  int r;
  rep(i, n) if (T[i].p == NIL) r = i;
  rec(r, 0);
  rep(i, n) print(i);
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define EPS 1e-10                         // sample:if((double)>=(double)+EPS)
#define CLR(a) memset((&a), 0, sizeof(a)) // clear memory
#define dump(x) cerr << #x << " = " << (x) << endl; // debug
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
// cin,cout release
// std::cin.tie(0);
// ios::sync_with_stdio(false);
using namespace std;

struct node {
  int id, parent, left, right;
};
typedef struct node *bint;
struct node T[10000];
void preperse(int u) {
  if (u == -1)
    return;
  printf(" %d", u);
  preperse(T[u].left);
  preperse(T[u].right);
}
void inperse(int u) {
  if (u == -1)
    return;
  inperse(T[u].left);
  printf(" %d", u);
  inperse(T[u].right);
}
void postperse(int u) {
  if (u == -1)
    return;
  postperse(T[u].left);
  postperse(T[u].right);
  printf(" %d", u);
}

int main() {
  int n, id, l, r;
  cin >> n;
  for (int i = 0; i < 10000; i++)
    T[i].parent = -1;
  for (int i = 0; i < n; i++) {
    cin >> id >> l >> r;
    T[id].left = l;
    T[id].right = r;
    if (l != -1)
      T[l].parent = id;
    if (r != -1)
      T[r].parent = id;
  }
  int root;
  for (int i = 0; i < n; i++)
    if (T[i].parent == -1)
      root = i;

  cout << "Preorder" << endl;
  preperse(root);
  cout << endl << "Inorder" << endl;
  inperse(root);
  cout << endl << "Postorder" << endl;
  postperse(root);
  cout << endl;

  return 0;
}