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

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
using namespace std;
const int num = 25, NIL = -1;
struct Node {
  int l, r, p, si;
};
Node T[num];
int Dp[num], Dg[num], H[num];
inline void getDepth(int u, int p) {
  Dp[u] = p;
  if (T[u].l != NIL)
    getDepth(T[u].l, p + 1);
  if (T[u].r != NIL)
    getDepth(T[u].r, p + 1);
}
inline void getDegree(int u) {
  Dg[u] = 0;
  if (T[u].l != NIL)
    Dg[u]++;
  if (T[u].r != NIL)
    Dg[u]++;
}
inline int getHeight(int u) {
  int h1 = 0, h2 = 0;
  if (T[u].l != NIL)
    h1 = getHeight(T[u].l);
  if (T[u].r != NIL)
    h2 = getHeight(T[u].r);
  H[u] = max(h1, h2);
  return H[u];
}
int main() {
  int n, rootnum, h = 0, u;
  cin >> n;
  for (int i = 0; i < n; i++)
    T[i].l = T[i].r = T[i].p = T[i].si = H[i] = NIL;
  for (int i = 0; i < n; i++) {
    cin >> u;
    cin >> T[u].l >> T[u].r;
  }
  for (int i = 0; i < n; i++) {

    T[T[i].l].si = T[i].r;
    T[T[i].r].si = T[i].l;
    T[T[i].l].p = i;
    T[T[i].r].p = i;
  }
  for (int i = 0; i < n; i++)
    if (T[i].p == NIL)
      rootnum = i;
  getDepth(rootnum, 0);
  for (int i = 0; i < n; i++)
    getDegree(i);
  for (int i = 0; i < n; i++) {
    if (T[i].l == NIL && T[i].r == NIL)
      H[i] = h;
  }
  for (int j = 0; j < 13; j++) {
    for (int i = 0; i < n; i++) {
      if (H[i] != 0 && H[T[i].l] != NIL && H[T[i].r] != NIL)
        H[i] = 1 + max(H[T[i].l], H[T[i].r]);
    }
    if (H[rootnum] != NIL)
      break;
  }
  // getHeight(rootnum);
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << T[i].p << ", sibling = " << T[i].si
         << ", degree = " << Dg[i] << ", depth = " << Dp[i]
         << ", height = " << H[i] << ", ";
    if (T[i].p == NIL)
      cout << "root" << endl;
    else if (T[i].l == NIL && T[i].r == NIL)
      cout << "leaf" << endl;
    else
      cout << "internal node" << endl;
  }
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
using namespace std;
const int num = 25, NIL = -1;
struct Node {
  int l, r, p, si;
};
Node T[num];
int Dp[num], Dg[num], H[num];
inline void getDepth(int u, int p) {
  Dp[u] = p;
  if (T[u].l != NIL) {
    getDepth(T[u].l, p + 1);
    getDepth(T[u].r, p + 1);
  }
}
inline void getDegree(int u) {
  if (T[u].l != NIL)
    Dg[u] = 2;
  else
    Dg[u] = 0;
}
inline int getHeight(int u) {
  int h1 = 0, h2 = 0;
  if (T[u].l != NIL)
    h1 = getHeight(T[u].l);
  if (T[u].r != NIL)
    h2 = getHeight(T[u].r);
  H[u] = max(h1, h2);
  return H[u];
}
int main() {
  int n, rootnum, h = 0, u;
  cin >> n;
  for (int i = 0; i < n; i++)
    T[i].l = T[i].r = T[i].p = T[i].si = H[i] = NIL;
  for (int i = 0; i < n; i++) {
    cin >> u;
    cin >> T[u].l >> T[u].r;
  }
  for (int i = 0; i < n; i++) {
    T[T[i].l].si = T[i].r;
    T[T[i].r].si = T[i].l;
    T[T[i].l].p = i;
    T[T[i].r].p = i;
  }
  for (int i = 0; i < n; i++)
    if (T[i].p == NIL)
      rootnum = i;
  getDepth(rootnum, 0);
  for (int i = 0; i < n; i++)
    getDegree(i);
  for (int i = 0; i < n; i++) {
    if (T[i].l == NIL)
      H[i] = h;
  }
  for (int j = 0; j < 13; j++) {
    for (int i = 0; i < n; i++) {
      if (H[i] != 0 && H[T[i].l] != NIL && H[T[i].r] != NIL)
        H[i] = 1 + max(H[T[i].l], H[T[i].r]);
    }
    if (H[rootnum] != NIL)
      break;
  }
  // getHeight(rootnum);
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << T[i].p << ", sibling = " << T[i].si
         << ", degree = " << Dg[i] << ", depth = " << Dp[i]
         << ", height = " << H[i] << ", ";
    if (T[i].p == NIL)
      cout << "root" << endl;
    else if (T[i].l == NIL)
      cout << "leaf" << endl;
    else
      cout << "internal node" << endl;
  }
}