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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> parent;
vector<int> L;
vector<int> R;
vector<int> d(10005);
vector<int> h(10005);

int n;

void Depth(int i, int x) {
  if (i == -1)
    return;
  d[i] = x;
  Depth(L[i], x + 1);
  Depth(R[i], x + 1);
}

int Height(int i) {
  int h1 = 0;
  int h2 = 0;

  if (L[i] != -1)
    h1 = Height(L[i]) + 1;
  if (R[i] != -1)
    h2 = Height(R[i]) + 1;

  return h[i] = max(h1, h2);
}

int Sibling(int i) {

  if (parent[i] == -1)
    return -1;
  if (L[parent[i]] != i && L[parent[i]] != -1) {
    return L[parent[i]];
  }

  if (R[parent[i]] != i && R[parent[i]] != -1) {
    return R[parent[i]];
  }

  return -1;
}

void print(int i) {
  cout << "node " << i << ": ";
  cout << "parent = " << parent[i] << ", ";
  cout << "sibling = " << Sibling(i) << ", ";

  int cnt = 0;

  if (L[i] != -1)
    cnt++;
  if (R[i] != -1)
    cnt++;
  cout << "degree = " << cnt << ", ";
  cout << "depth = " << d[i] << ", ";
  cout << "height = " << h[i] << ", ";

  if (parent[i] == -1)
    cout << "root" << endl;
  else if (L[i] == -1 && R[i] == -1)
    cout << "leaf" << endl;
  else
    cout << "internal node" << endl;
}

int main() {
  int id, l, r, root = 0;
  cin >> n;

  parent.resize(n);
  R.resize(n);
  L.resize(n);

  for (int i = 0; i < n; i++)
    parent[i] = -1;

  for (int i = 0; i < n; i++) {
    cin >> id >> l >> r;
    L[id] = l;
    R[id] = r;

    if (l != -1)
      parent[l] = id;
    if (r != -1)
      parent[r] = id;
  }

  for (int i = 0; i < n; i++)
    if (parent[i] == -1)
      root = i;

  Depth(root, 0);
  Height(root);

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

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> parent;
vector<int> L;
vector<int> R;
vector<int> d(10005);
vector<int> h(10005);

int n;

void Depth(int i, int x) {
  if (i == -1)
    return;
  d[i] = x;
  Depth(L[i], x + 1);
  Depth(R[i], x + 1);
}

int Height(int i) {
  int h1 = 0;
  int h2 = 0;

  if (L[i] != -1)
    h1 = Height(L[i]) + 1;
  if (R[i] != -1)
    h2 = Height(R[i]) + 1;

  return h[i] = max(h1, h2);
}

int Sibling(int i) {

  if (parent[i] == -1)
    return -1;
  if (L[parent[i]] != i && L[parent[i]] != -1) {
    return L[parent[i]];
  }

  if (R[parent[i]] != i && R[parent[i]] != -1) {
    return R[parent[i]];
  }

  return -1;
}

void print(int i) {
  cout << "node " << i << " ";
  cout << "parent = " << parent[i] << " ";
  cout << "sibling = " << Sibling(i) << " ";

  int cnt = 0;

  if (L[i] != -1)
    cnt++;
  if (R[i] != -1)
    cnt++;
  cout << "degree = " << cnt << " ";
  cout << "depth = " << d[i] << " ";
  cout << "height = " << h[i] << " ";

  if (parent[i] == -1)
    cout << "root" << endl;
  else if (L[i] == -1 && R[i] == -1)
    cout << "leaf" << endl;
  else
    cout << "internal node" << endl;
}

int main() {
  int id, l, r, root = 0;
  cin >> n;

  for (int i = 0; i < n; i++)
    parent[i] = -1;

  for (int i = 0; i < n; i++) {
    cin >> id >> l >> r;
    L[id] = l;
    R[id] = r;

    if (l != -1)
      parent[l] = id;
    if (r != -1)
      parent[r] = id;
  }

  for (int i = 0; i < n; i++)
    if (parent[i] == -1)
      root = i;

  Depth(root, 0);
  Height(root);

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

  return 0;
}