Problem p02280 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

#define MAX 10000
#define NIL -1

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

Node T[MAX];
int D[MAX], H[MAX];

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

int setHeight(int u) {
  int h1 = 0, 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] = (h1 > h2 ? h1 : h2);
}

int getSibling(int u) {
  if (T[u].parent == NIL) {
    return NIL;
  }
  if (T[T[u].parent].left != u && T[T[u].parent].left != NIL) {
    return T[T[u].parent].left;
  }
  if (T[T[u].parent].right != u && T[T[u].parent].right != NIL) {
    return T[T[u].parent].right;
  }
  return NIL;
}

void print(int u) {
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].parent << ", ";
  cout << "sibling = " << getSibling(u) << ", ";
  int deg = 0;
  if (T[u].left != NIL) {
    deg++;
  }
  if (T[u].right != NIL) {
    deg++;
  }

  cout << "degree = " << deg << ", ";
  cout << "depth = " << D[u] << ", ";
  cout << "height = " << H[u] << ", ";

  if (T[u].parent == NIL) {
    cout << "root" << endl;
  } else if (T[u].left == NIL && T[u].right == NIL) {
    cout << "leaf" << endl;
  } else {
    cout << "internal node" << endl;
  }
}

int main() {
  int N;
  cin >> N;

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

  int v, left, right;
  for (int i = 0; i < N; i++) {
    cin >> v >> left, right;
    T[v].left = left;
    T[v].right = right;
    if (left != NIL) {
      T[left].parent = v;
    }
    if (right != NIL) {
      T[right].parent = v;
    }
  }

  int root = 0;
  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++) {
    print(i);
  }

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

#define MAX 10000
#define NIL -1

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

Node T[MAX];
int D[MAX], H[MAX];

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

int setHeight(int u) {
  int h1 = 0, 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] = (h1 > h2 ? h1 : h2);
}

int getSibling(int u) {
  if (T[u].parent == NIL) {
    return NIL;
  }
  if (T[T[u].parent].left != u && T[T[u].parent].left != NIL) {
    return T[T[u].parent].left;
  }
  if (T[T[u].parent].right != u && T[T[u].parent].right != NIL) {
    return T[T[u].parent].right;
  }
  return NIL;
}

void print(int u) {
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].parent << ", ";
  cout << "sibling = " << getSibling(u) << ", ";
  int deg = 0;
  if (T[u].left != NIL) {
    deg++;
  }
  if (T[u].right != NIL) {
    deg++;
  }

  cout << "degree = " << deg << ", ";
  cout << "depth = " << D[u] << ", ";
  cout << "height = " << H[u] << ", ";

  if (T[u].parent == NIL) {
    cout << "root" << endl;
  } else if (T[u].left == NIL && T[u].right == NIL) {
    cout << "leaf" << endl;
  } else {
    cout << "internal node" << endl;
  }
}

int main() {
  int N;
  cin >> N;

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

  int v, left, right;
  for (int i = 0; i < N; i++) {
    cin >> v >> left >> right;
    T[v].left = left;
    T[v].right = right;
    if (left != NIL) {
      T[left].parent = v;
    }
    if (right != NIL) {
      T[right].parent = v;
    }
  }

  int root = 0;
  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++) {
    print(i);
  }

  return 0;
}