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

#include <bits/stdc++.h>
using namespace std;
#define MAX 30
#define NIL -1
int n, D[MAX], H[MAX];

struct node {
  int p, l, r;
};
node T[MAX];

void printTree() {
  int parent;
  for (int i = 0; i < n; ++i) {
    printf("node %d: parent = %d, ", i, T[i].p);

    if (T[i].p == NIL)
      printf("sibling = -1, ");
    else {
      parent = T[i].p;
      if (T[parent].l == i) {
        if (T[parent].r == NIL)
          printf("sibling = -1, ");
        else
          printf("sibling = %d, ", T[parent].r);
      } else if (T[parent].r == i) {
        if (T[parent].l == NIL)
          printf("sibling = -1, ");
        else
          printf("sibling = %d, ", T[parent].l);
      }
    }

    if (T[i].l == NIL && T[i].r == NIL)
      printf("degree = 0, ");
    else if (T[i].l != NIL && T[i].r != NIL)
      printf("degree = 2, ");
    else if (T[i].l != NIL && T[i].r == NIL)
      printf("degree = 1, ");
    else if (T[i].l == NIL && T[i].r != NIL)
      printf("degree = 1, ");

    printf("depth = %d, height = %d, ", D[i], H[i]);

    if (T[i].p == NIL)
      printf("root");
    else {
      if (T[i].l == NIL && T[i].r == NIL)
        printf("leaf");
      else
        printf("internal node");
    }
    printf("\\n");
  }
}

void recDepth(int pos, int depth) {
  D[pos] = depth;
  if (T[pos].l != NIL) {
    recDepth(T[pos].l, depth + 1);
  }
  if (T[pos].r != NIL) {
    recDepth(T[pos].r, depth + 1);
  }
}

int recHeight(int pos) {
  int h1 = 0, h2 = 0;
  if (T[pos].l != NIL) {
    h1 = recHeight(T[pos].l) + 1;
  }
  if (T[pos].r != NIL) {
    h2 = recHeight(T[pos].r) + 1;
  }
  H[pos] = (h1 > h2) ? h1 : h2;
  return H[pos];
}

int main() {

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    T[i].l = T[i].p = T[i].r = NIL;
    D[i] = 0;
  }
  int id, lchild, rchild, sib, root;

  for (int i = 0; i < n; ++i) {
    scanf("%d %d %d", &id, &lchild, &rchild);
    T[id].l = lchild;
    T[id].r = rchild;
    T[lchild].p = id;
    T[rchild].p = id;
  }
  for (int i = 0; i < n; ++i) {
    if (T[i].p == NIL)
      root = i;
  }
  recDepth(root, 0);
  recHeight(root);
  printTree();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX 30
#define NIL -1
int n, D[MAX], H[MAX];

struct node {
  int p, l, r;
};
node T[MAX];

void printTree() {
  int parent;
  for (int i = 0; i < n; ++i) {
    printf("node %d: parent = %d, ", i, T[i].p);

    if (T[i].p == NIL)
      printf("sibling = -1, ");
    else {
      parent = T[i].p;
      if (T[parent].l == i) {
        if (T[parent].r == NIL)
          printf("sibling = -1, ");
        else
          printf("sibling = %d, ", T[parent].r);
      } else if (T[parent].r == i) {
        printf("sibling = %d, ", T[parent].l);
      }
    }

    if (T[i].l == NIL)
      printf("degree = 0, ");
    if (T[i].r != NIL)
      printf("degree = 2, ");
    if (T[i].l != NIL && T[i].r == NIL)
      printf("degree = 1, ");

    printf("depth = %d, height = %d, ", D[i], H[i]);

    if (T[i].p == NIL)
      printf("root");
    else {
      if (T[i].l == NIL)
        printf("leaf");
      else
        printf("internal node");
    }
    printf("\\n");
  }
}

void recDepth(int pos, int depth) {
  D[pos] = depth;
  if (T[pos].l != NIL) {
    recDepth(T[pos].l, depth + 1);
  }
  if (T[pos].r != NIL) {
    recDepth(T[pos].r, depth + 1);
  }
}

int recHeight(int pos) {
  int h1 = 0, h2 = 0;
  if (T[pos].l != NIL) {
    h1 = recHeight(T[pos].l) + 1;
  }
  if (T[pos].r != NIL) {
    h2 = recHeight(T[pos].r) + 1;
  }
  H[pos] = (h1 > h2) ? h1 : h2;
  return H[pos];
}

int main() {

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    T[i].l = T[i].p = T[i].r = NIL;
    D[i] = 0;
  }
  int id, lchild, rchild, sib, root;

  for (int i = 0; i < n; ++i) {
    cin >> id >> lchild >> rchild;
    T[id].l = lchild;
    T[id].r = rchild;
    T[lchild].p = id;
    T[rchild].p = id;
  }
  for (int i = 0; i < n; ++i) {
    if (T[i].p == NIL)
      root = i;
  }
  recDepth(root, 0);
  recHeight(root);
  printTree();
  return 0;
}