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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define fori(i, k, n) for (int i = k; i < (int)n; i++)
using namespace std;
int stringInt(string a) { return atoi(a.c_str()); }
string intString(int a) {
  stringstream ss;
  ss << a;
  return ss.str();
}
vector<int> SieveOfEratosthenes(int x) {
  vector<int> a;
  vector<int> b(x + 1, 0);
  for (int i = 2; i <= x; i++) {
    if (b[i] == 0) {
      a.push_back(i);
      for (int l = i; l <= x; l += i) {
        b[l] = 1;
      }
    }
  }
  return a;
}
struct node {
  int p, l, r;
};
int n;
node a[30];
string typ[30];
int sib(int x) {
  if (a[x].p == -1)
    return -1;
  if (a[a[x].p].l == x) {
    return a[x].r;
  } else {
    return a[a[x].p].l;
  }
}
int deg(int x) {
  int ans = 0;
  if (a[x].l != -1) {
    ans++;
    if (a[a[x].l].r != -1)
      ans++;
  }
  return ans;
}
int dep(int x) {
  int ans = 0;
  int y = x;
  while (1) {
    if (a[y].p == -1)
      break;
    ans++;
    y = a[y].p;
  }
  return ans;
}
int h(int x) {

  int ans = 0;
  if (a[x].l == -1)
    return ans;
  int h1 = -1, h2 = -1;
  if (a[x].l != -1)
    h1 = h(a[x].l);
  if (a[a[x].l].r != -1)
    h2 = h(a[a[x].l].r);
  ans = max(h1, h2) + 1;

  return ans;
}
void print(int x) {
  cout << "node " << x << ": ";
  cout << "parent = " << a[x].p << ", ";
  cout << "sibling = " << sib(x) << ", ";
  cout << "degree = " << deg(x) << ", ";
  cout << "depth = " << dep(x) << ", ";
  cout << "height = " << h(x) << ", ";
  cout << typ[x] << endl;
}
int main() {

  rep(i, 30) a[i].p = a[i].l = a[i].r = -1;
  cin >> n;
  rep(i, n) {
    int id, R, L;
    cin >> id >> R >> L;
    if (R < L)
      swap(R, L);
    a[id].l = R;
    if (R != -1) {
      a[R].p = id;
      a[R].r = L;
    }
    if (L != -1)
      a[L].p = id;
  }
  rep(i, n) {
    if (a[i].p == -1) {
      typ[i] = "root";

    } else if (a[i].l == -1) {
      typ[i] = "leaf";

    } else {
      typ[i] = "internal node";
    }
  }

  rep(i, n) { print(i); }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define fori(i, k, n) for (int i = k; i < (int)n; i++)
using namespace std;
int stringInt(string a) { return atoi(a.c_str()); }
string intString(int a) {
  stringstream ss;
  ss << a;
  return ss.str();
}
vector<int> SieveOfEratosthenes(int x) {
  vector<int> a;
  vector<int> b(x + 1, 0);
  for (int i = 2; i <= x; i++) {
    if (b[i] == 0) {
      a.push_back(i);
      for (int l = i; l <= x; l += i) {
        b[l] = 1;
      }
    }
  }
  return a;
}
struct node {
  int p, l, r;
};
int n;
node a[30];
string typ[30];
int sib(int x) {
  if (a[x].p == -1)
    return -1;
  if (a[a[x].p].l == x) {
    return a[x].r;
  } else {
    return a[a[x].p].l;
  }
}
int deg(int x) {
  int ans = 0;
  if (a[x].l != -1) {
    ans++;
    if (a[a[x].l].r != -1)
      ans++;
  }
  return ans;
}
int dep(int x) {
  int ans = 0;
  int y = x;
  while (1) {
    if (a[y].p == -1)
      break;
    ans++;
    y = a[y].p;
  }
  return ans;
}
int h(int x) {
  int ans = 0;
  if (typ[x] == "leaf")
    return ans;
  int h1 = -1, h2 = -1;
  h1 = h(a[x].l);
  if (a[a[x].l].r != -1)
    h2 = h(a[a[x].l].r);
  ans = max(h1, h2) + 1;

  return ans;
}
void print(int x) {
  cout << "node " << x << ": ";
  cout << "parent = " << a[x].p << ", ";
  cout << "sibling = " << sib(x) << ", ";
  cout << "degree = " << deg(x) << ", ";
  cout << "depth = " << dep(x) << ", ";
  cout << "height = " << h(x) << ", ";
  cout << typ[x] << endl;
}
int main() {

  rep(i, 30) a[i].p = a[i].l = a[i].r = -1;
  cin >> n;
  rep(i, n) {
    int id, R, L;
    cin >> id >> R >> L;
    a[id].l = R;
    if (R != -1) {
      a[R].p = id;
      a[R].r = L;
    }
    if (L != -1)
      a[L].p = id;
  }
  rep(i, n) {
    if (a[i].p == -1) {
      typ[i] = "root";

    } else if (a[i].l == -1) {
      typ[i] = "leaf";

    } else {
      typ[i] = "internal node";
    }
  }

  rep(i, n) { print(i); }
}