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

#include <bits/stdc++.h>
#define NIL -1
#define MAX 30
using namespace std;

struct Node {
  int parent, left, right, sibling;
};

Node defaultNode = {NIL, NIL, NIL, NIL};

vector<Node> T(MAX, defaultNode);

vector<int> D(MAX, 0);
vector<int> H(MAX, 0);

void setDepth(int u, int p) {
  D[u] = p;
  if (T[u].left != NIL) {
    setDepth(T[u].left, p + 1);
  }
  if (T[u].right != NIL) {
    setDepth(T[u].right, p + 1);
  }
};

int setHeight(int u) {
  int h1, h2;
  h1 = h2 = 0;
  if (T[u].left != NIL) {
    h1 = setHeight(T[u].left) + 1;
  }
  if (T[u].right != NIL) {
    h2 = setHeight(T[u].right) + 1;
  }
  return H[u] = max(h1, h2);
};

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int u, l, r;
    cin >> u >> l >> r;
    T[u].left = l;
    T[u].right = r;
    if (l != NIL) {
      T[l].parent = u;
      if (r != NIL)
        T[l].sibling = r;
    }
    if (r != NIL) {
      T[r].parent = u;
      if (l != NIL)
        T[r].sibling = l;
    }
  }

  int root;

  for (int i = 0; i < n; i++) {
    if (T[i].parent == NIL)
      root = i;
  }

  setDepth(root, 0);
  setHeight(root);

  for (int i = 0; i < n; i++) {
    int degree = 0;
    if (T[i].left != NIL)
      degree++;
    if (T[i].right != NIL)
      degree++;
    cout << "node " << i << ": parent = " << T[i].parent
         << ", sibling = " << T[i].sibling << ", degree = " << degree
         << ", depth = " << D[i] << ", height = " << H[i] << ", ";
    if (T[i].parent == NIL)
      cout << "root" << endl;
    else if (T[i].left == NIL && T[i].right == NIL)
      cout << "leaf" << endl;
    else
      cout << "internal node" << endl;
  }
};


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define NIL -1
#define MAX 30
using namespace std;

struct Node {
  int parent, left, right, sibling;
};

Node defaultNode = {NIL, NIL, NIL, NIL};

vector<Node> T(MAX, defaultNode);

vector<int> D(MAX, 0);
vector<int> H(MAX, 0);

void setDepth(int u, int p) {
  D[u] = p;
  if (T[u].left != NIL) {
    setDepth(T[u].left, p + 1);
  }
  if (T[u].right != NIL) {
    setDepth(T[u].right, p + 1);
  }
};

int setHeight(int u) {
  int h1, h2;
  h1 = h2 = 0;
  if (T[u].left != NIL) {
    h1 = setHeight(T[u].left) + 1;
  }
  if (T[u].right != NIL) {
    h2 = setHeight(T[u].right) + 1;
  }
  return H[u] = max(h1, h2);
};

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int u, l, r;
    cin >> u >> l >> r;
    T[u].left = l;
    T[u].right = r;
    if (l != NIL) {
      T[l].parent = T[r].parent = u;
      T[l].sibling = r;
      T[r].sibling = l;
    }
  }

  int root;

  for (int i = 0; i < n; i++) {
    if (T[i].parent == NIL)
      root = i;
  }

  setDepth(root, 0);
  setHeight(root);

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << T[i].parent
         << ", sibling = " << T[i].sibling << ", degree = ";
    if (T[i].left == NIL)
      cout << 0;
    else
      cout << 2;
    cout << ", depth = " << D[i] << ", height = " << H[i] << ", ";
    if (T[i].parent == NIL)
      cout << "root" << endl;
    else if (T[i].left == NIL)
      cout << "leaf" << endl;
    else
      cout << "internal node" << endl;
  }
};