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

// binary trees
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int n, par[30], sib[30], deg[30] = {}, dep[30], hei[30], typ[30],
                         chi[30][2]; // root=1,internal node=2,leaf=3;//l/r
vector<int> leaf;

int depth(int now) {
  if (dep[now] != -1)
    return dep[now];
  if (par[now] == -1)
    return 0;
  return dep[now] = depth(par[now]) + 1;
}
int height(int now) {
  if (hei[now] != -1)
    return hei[now];
  if (deg[now] == 0)
    return 0;
  else if (chi[now][0] * chi[now][1] < 0) {
    if (chi[now][0] != -1)
      return hei[now] = height(chi[now][0]) + 1;
    else
      return hei[now] = height(chi[now][1]) + 1;
  } else
    return hei[now] = max(height(chi[now][0]) + 1, height(chi[now][1]) + 1);
}

int main() {
  // input,clear
  cin >> n; // node
  for (int i = 0; i < n; i++) {
    par[i] = -1;
    dep[i] = -1;
    hei[i] = -1;
    chi[i][0] = -1;
    chi[i][1] = -1;
  }

  for (int i = 0; i < n; i++) {
    int id, le, ri;
    cin >> id >> le >> ri;
    if (le == -1 && ri == -1) {
      typ[id] = 3; // no child>>leaf
      hei[id] = 0;
    } else {
      typ[id] = 2;
      // 親に子のデータ  子に親のデータ

      if (le != -1) {
        deg[id]++;
        chi[id][0] = le;
        par[le] = id;
        sib[le] = ri;
      }
      if (ri != -1) {
        deg[id]++;
        chi[id][1] = ri;
        par[ri] = id;
        sib[ri] = le;
      }
    }
  }
  // input end

  for (int i = 0; i < n; i++) {
    if (par[i] == -1) {
      typ[i] = 1; // root
      sib[i] = -1;
    }
    cout << "node " << i << ": ";
    cout << "parent = " << par[i] << ", ";
    cout << "sibling = " << sib[i] << ", ";
    cout << "degree = " << deg[i] << ", ";
    cout << "depth = " << depth(i) << ", ";
    cout << "height = " << height(i) << ", ";
    if (typ[i] == 1)
      cout << "root";
    else if (typ[i] == 2)
      cout << "internal node";
    else
      cout << "leaf";
    cout << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

// binary trees
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int n, par[30], sib[30],
    deg[30] = {}, dep[30], hei[30], typ[30],
    chi[30][2] = {-1}; // root=1,internal node=2,leaf=3;//l/r
vector<int> leaf;

int depth(int now) {
  if (dep[now] != -1)
    return dep[now];
  if (par[now] == -1)
    return 0;
  return dep[now] = depth(par[now]) + 1;
}
int height(int now) {
  if (hei[now] != -1)
    return hei[now];
  if (deg[now] == 0)
    return 0;
  else if (chi[now][0] * chi[now][1] < 0) {
    if (chi[now][0] != -1)
      return hei[now] = height(chi[now][0]) + 1;
    else
      return hei[now] = height(chi[now][1]) + 1;
  } else
    return hei[now] = max(height(chi[now][0]) + 1, height(chi[now][1]) + 1);
}

int main() {
  // input,clear
  cin >> n; // node
  for (int i = 0; i < n; i++) {
    par[i] = -1;
    dep[i] = -1;
    hei[i] = -1;
  }

  for (int i = 0; i < n; i++) {
    int id, le, ri;
    cin >> id >> le >> ri;
    if (le == -1 && ri == -1) {
      typ[id] = 3; // no child>>leaf
      hei[id] = 0;
    } else {
      typ[id] = 2;
      // 親に子のデータ  子に親のデータ

      if (le != -1) {
        deg[id]++;
        chi[id][0] = le;
        par[le] = id;
        sib[le] = ri;
      }
      if (ri != -1) {
        deg[id]++;
        chi[id][1] = ri;
        par[ri] = id;
        sib[ri] = le;
      }
    }
  }
  // input end

  for (int i = 0; i < n; i++) {
    if (par[i] == -1) {
      typ[i] = 1; // root
      sib[i] = -1;
    }
    cout << "node " << i << ": ";
    cout << "parent = " << par[i] << ", ";
    cout << "sibling = " << sib[i] << ", ";
    cout << "degree = " << deg[i] << ", ";
    cout << "depth = " << depth(i) << ", ";
    cout << "height = " << height(i) << ", ";
    if (typ[i] == 1)
      cout << "root";
    else if (typ[i] == 2)
      cout << "internal node";
    else
      cout << "leaf";
    cout << endl;
  }

  return 0;
}