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

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

int n, place;

struct node {
  int id, pid, bid, depth, degree, h;
  int lc, rc;
  string type;
} N[10000];

void Set() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    N[i].pid = -1;
    N[i].bid = -1;
    N[i].h = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> place;
    N[place].id = place;

    cin >> N[place].lc >> N[place].rc;
    N[N[place].lc].id = N[place].lc;
    N[N[place].rc].id = N[place].rc;

    if (N[place].lc != -1 && N[place].rc != -1) {
      N[N[place].lc].bid = N[N[place].rc].id;
      N[N[place].rc].bid = N[N[place].lc].id;

      N[N[place].lc].pid = N[place].id;
      N[N[place].rc].pid = N[place].id;

      N[place].degree = 2;
    } else if (N[place].lc == -1 && N[place].rc == -1)
      N[place].degree = 0;
    else {
      if (N[place].lc != -1)
        N[N[place].lc].pid = N[place].id;
      else
        N[N[place].rc].pid = N[place].id;

      N[place].degree = 1;
    }
  }
}

void GetDepth(node &GN) {
  GN.depth = 0;
  node temp = GN;

  while (temp.pid != -1) {
    GN.depth++;
    temp = N[temp.pid];
  }
}

int GetHeight(node &GN) {
  int h1 = 0, h2 = 0;
  if (GN.lc != -1)
    h1 = GetHeight(N[GN.lc]) + 1;
  if (GN.rc != -1)
    h2 = GetHeight(N[GN.rc]) + 1;
  return h1 > h2 ? h1 : h2;
}

void GetType(node &GN) {
  if (GN.pid == -1)
    GN.type = "root";
  else if (GN.degree == 0)
    GN.type = "leaf";
  else
    GN.type = "internal node";
}

void print() {
  for (int i = 0; i < n; i++) {
    cout << "node " << N[i].id << ": "
         << "parent = " << N[i].pid << ", ";
    cout << "sibling = " << N[i].bid << ", ";
    cout << "degree = " << N[i].degree << ", ";
    cout << "depth = " << N[i].depth << ", ";
    cout << "height = " << N[i].h << ", ";
    cout << N[i].type << endl;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  Set();

  for (int i = 0; i < n; i++) {
    GetDepth(N[i]);
    N[i].h = GetHeight(N[i]);
    GetType(N[i]);
  }

  print();

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int n, place;

struct node {
  int id, pid, bid, depth, degree, h;
  int lc, rc;
  string type;
} N[10000];

void Set() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    N[i].pid = -1;
    N[i].bid = -1;
    N[i].h = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> place;
    N[place].id = place;

    cin >> N[place].lc >> N[place].rc;
    N[N[place].lc].id = N[place].lc;
    N[N[place].rc].id = N[place].rc;

    if (N[place].lc != -1 && N[place].rc != -1) {
      N[N[place].lc].bid = N[N[place].rc].id;
      N[N[place].rc].bid = N[N[place].lc].id;

      N[N[place].lc].pid = N[place].id;
      N[N[place].rc].pid = N[place].id;

      N[place].degree = 2;
    } else if (N[place].lc == -1 && N[place].rc == -1)
      N[place].degree = 0;
    else {
      if (N[place].lc != -1)
        N[N[place].lc].pid = N[place].id;
      else
        N[N[place].rc].pid = N[place].id;

      N[place].degree = 1;
    }
  }
}

void GetDepth(node &GN) {
  GN.depth = 0;
  node temp = GN;

  while (temp.pid != -1) {
    GN.depth++;
    temp = N[temp.pid];
  }
}

void GetHeight(node GN, int i) {
  if (GN.degree != 0) {
    GN = N[GN.lc].degree > N[GN.rc].degree ? N[GN.lc] : N[GN.rc];
    GetHeight(GN, i);
    N[i].h++;
  }
}

void GetType(node &GN) {
  if (GN.pid == -1)
    GN.type = "root";
  else if (GN.degree == 0)
    GN.type = "leaf";
  else
    GN.type = "internal node";
}

void print() {
  for (int i = 0; i < n; i++) {
    cout << "node " << N[i].id << ": "
         << "parent = " << N[i].pid << ", ";
    cout << "sibling = " << N[i].bid << ", ";
    cout << "degree = " << N[i].degree << ", ";
    cout << "depth = " << N[i].depth << ", ";
    cout << "height = " << N[i].h << ", ";
    cout << N[i].type << endl;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  Set();

  for (int i = 0; i < n; i++) {
    GetDepth(N[i]);
    GetHeight(N[i], i);
    GetType(N[i]);
  }

  print();

  return 0;
}