Problem p02280 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;

const int N = 25;
const int NIL = -1;

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

void set_depth(int id, int dep, int depth[], Node T[]) {
  depth[id] = dep;
  if (T[id].left != NIL)
    set_depth(T[id].left, dep + 1, depth, T);
  if (T[id].right != NIL)
    set_depth(T[id].right, dep + 1, depth, T);
}

void set_type(int n, string type[], Node T[]) {
  for (int i = 0; i < n; i++) {
    if (T[i].parent == NIL)
      type[i] = "root";
    else if (T[i].left == NIL && T[i].right == NIL)
      type[i] = "leaf";
    else
      type[i] = "internal node";
  }
}

int find_root(int n, string type[]) {
  int root = 0;
  for (int i = 0; i < n; i++) {
    if (type[i] == "root") {
      root = i;
      break;
    }
  }
  return root;
}

int set_height(int id, int height[], string type[], Node T[]) {
  if (type[id] == "leaf") {
    height[id] = 0;
    return 0;
  }
  int h, h1, h2;
  h1 = 1 + set_height(T[id].left, height, type, T);
  h2 = 1 + set_height(T[id].right, height, type, T);
  h = max(h1, h2);
  height[id] = h;
  return h;
}

int main() {
  int i, n, id, left, right;
  Node T[N];
  int sibling[N], degree[N], depth[N], height[N];
  string type[N];

  cin >> n;

  // init
  for (i = 0; i < n; i++) {
    T[i].parent = NIL;
    T[i].left = NIL;
    T[i].right = NIL;
    sibling[i] = NIL;
  }

  // input
  for (i = 0; i < n; i++) {
    cin >> id >> left >> right;
    T[id].left = left;
    T[id].right = right;
    if (left != NIL && right != NIL) {
      T[left].parent = id;
      T[right].parent = id;
      sibling[left] = right;
      sibling[right] = left;
      degree[id] = 2;
    } else if (left == NIL && right != NIL) {
      T[right].parent = id;
      sibling[right] = left;
      degree[id] = 1;
    } else if (left != NIL && right == NIL) {
      T[left].parent = id;
      sibling[left] = right;
      degree[id] = 1;
    } else {
      degree[id] = 0;
    }
  }

  set_type(n, type, T);
  int root = find_root(n, type);
  set_depth(root, 0, depth, T);
  set_height(root, height, type, T);

  // output
  for (i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << T[i].parent
         << ", sibling = " << sibling[i] << ", degree = " << degree[i]
         << ", depth = " << depth[i] << ", height = " << height[i] << ", "
         << type[i] << endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;

const int N = 25;
const int NIL = -1;

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

void set_depth(int id, int dep, int depth[], Node T[]) {
  depth[id] = dep;
  if (T[id].left != NIL)
    set_depth(T[id].left, dep + 1, depth, T);
  if (T[id].right != NIL)
    set_depth(T[id].right, dep + 1, depth, T);
}

void set_type(int n, string type[], Node T[]) {
  for (int i = 0; i < n; i++) {
    if (T[i].parent == NIL)
      type[i] = "root";
    else if (T[i].left == NIL && T[i].right == NIL)
      type[i] = "leaf";
    else
      type[i] = "internal node";
  }
}

int find_root(int n, string type[]) {
  int root = 0;
  for (int i = 0; i < n; i++) {
    if (type[i] == "root") {
      root = i;
      break;
    }
  }
  return root;
}

int set_height(int id, int height[], string type[], Node T[]) {
  if (type[id] == "leaf") {
    height[id] = 0;
    return 0;
  }
  int h, h1 = 0, h2 = 0;
  if (T[id].left != NIL)
    h1 = 1 + set_height(T[id].left, height, type, T);
  if (T[id].right != NIL)
    h2 = 1 + set_height(T[id].right, height, type, T);
  h = max(h1, h2);
  height[id] = h;
  return h;
}

int main() {
  int i, n, id, left, right;
  Node T[N];
  int sibling[N], degree[N], depth[N], height[N];
  string type[N];

  cin >> n;

  // init
  for (i = 0; i < n; i++) {
    T[i].parent = NIL;
    T[i].left = NIL;
    T[i].right = NIL;
    sibling[i] = NIL;
  }

  // input
  for (i = 0; i < n; i++) {
    cin >> id >> left >> right;
    T[id].left = left;
    T[id].right = right;
    if (left != NIL && right != NIL) {
      T[left].parent = id;
      T[right].parent = id;
      sibling[left] = right;
      sibling[right] = left;
      degree[id] = 2;
    } else if (left == NIL && right != NIL) {
      T[right].parent = id;
      sibling[right] = left;
      degree[id] = 1;
    } else if (left != NIL && right == NIL) {
      T[left].parent = id;
      sibling[left] = right;
      degree[id] = 1;
    } else {
      degree[id] = 0;
    }
  }

  set_type(n, type, T);
  int root = find_root(n, type);
  set_depth(root, 0, depth, T);
  set_height(root, height, type, T);

  // output
  for (i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << T[i].parent
         << ", sibling = " << sibling[i] << ", degree = " << degree[i]
         << ", depth = " << depth[i] << ", height = " << height[i] << ", "
         << type[i] << endl;
  }

  return 0;
}