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

#include <algorithm>
#include <iostream>
using namespace std;
#define NIL -1
#define MAX 25
struct Node {
  int parent, left, right;
};

Node node[MAX];
int depth[MAX];
int height[MAX];

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

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

void print(int u) {
  cout << "node " << u << ": ";
  cout << "parent = " << node[u].parent << ", ";
  cout << "sibling = ";
  if (node[u].parent == NIL) {
    cout << "-1, ";
  } else if (node[node[u].parent].left == u) {
    cout << node[node[u].parent].right << ", ";
  } else {
    cout << node[node[u].parent].left << ", ";
  }

  cout << "degree = ";
  if (node[u].left != NIL && node[u].right != NIL) {
    cout << "2";
  } else if (node[u].left != NIL || node[u].right != NIL) {
    cout << "1";
  } else {
    cout << "0";
  }
  cout << ", ";
  cout << "depth = " << depth[u] << ", ";
  cout << "height = " << height[u] << ", ";
  if (node[u].parent == NIL) {
    cout << "root";
  } else if (node[u].left == NIL && node[u].right == NIL) {
    cout << "leaf";
  } else {
    cout << "internal node";
  }
  cout << endl;
}

int main() {
  int n;
  int id;
  int root = 0;

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

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

  for (int i = 0; i < n; i++) {
    print(i);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define NIL -1
#define MAX 25
struct Node {
  int parent, left, right;
};

Node node[MAX];
int depth[MAX];

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

int getHeight(int u) {
  if (node[u].left == NIL) {
    return 0;
  } else {
    return max(getHeight(node[u].left), getHeight(node[u].right)) + 1;
  }
}

void print(int u) {
  cout << "node " << u << ": ";
  cout << "parent = " << node[u].parent << ", ";
  cout << "sibling = ";
  if (node[u].parent == NIL) {
    cout << "-1, ";
  } else if (node[node[u].parent].left == u) {
    cout << node[node[u].parent].right << ", ";
  } else {
    cout << node[node[u].parent].left << ", ";
  }

  cout << "degree = ";
  if (node[u].left != NIL) {
    cout << "2";
  } else {
    cout << "0";
  }
  cout << ", ";
  cout << "depth = " << depth[u] << ", ";
  cout << "height = " << getHeight(u) << ", ";
  if (node[u].parent == -1) {
    cout << "root";
  } else if (node[u].left == -1) {
    cout << "leaf";
  } else {
    cout << "internal node";
  }
  cout << endl;
}

int main() {
  int n;
  int id;
  int root = 0;

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

  setDepth(root, 0);

  for (int i = 0; i < n; i++) {
    print(i);
  }
  return 0;
}