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

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
#define MAX 25
#define NIL -1

struct node {
  int p, l, r;
};

node A[MAX];

int D[MAX], H[MAX];

void setDepth(int v, int d) {
  if (v == NIL)
    return;
  D[v] = d;
  setDepth(A[v].l, d + 1);
  setDepth(A[v].r, d + 1);
}

int setHight(int v) {
  int h1 = 0, h2 = 0;
  if (A[v].l != NIL)
    h1 = setHight(A[v].l) + 1;
  if (A[v].r != NIL)
    h2 = setHight(A[v].r) + 1;
  return H[v] = max(h1, h2);
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    A[i].p = A[i].l = A[i].r = NIL;
  }
  int v, r, l, root;
  for (int i = 0; i < n; i++) {
    cin >> v >> l >> r;
    if (l != NIL) {
      A[v].l = l;
      A[l].p = v;
    }
    if (r != NIL) {
      A[v].r = r;
      A[r].p = v;
    }
  }
  for (int i = 0; i < n; i++) {
    if (A[i].p == NIL)
      root = i;
  }
  setDepth(root, 0);
  setHight(root);
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << A[i].p << ", sibling = ";
    if (A[i].p == NIL)
      cout << "-1";
    else if (A[A[i].p].l != i)
      cout << A[A[i].p].l;
    else
      cout << A[A[i].p].r;
    cout << ", degree = ";
    if (A[i].l == NIL && A[i].r == NIL)
      cout << "0, ";
    else if (A[i].l != NIL && A[i].r != NIL)
      cout << "2, ";
    else
      cout << "1, ";
    cout << "depth = " << D[i] << ", height = " << H[i] << ", ";
    if (D[i] == 0)
      cout << "root";
    else if (A[i].l == NIL && A[i].r == NIL)
      cout << "leaf";
    else
      cout << "internal node";
    cout << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
#define MAX 25
#define NIL -1

struct node {
  int p, l, r;
};

node A[MAX];

int D[MAX], H[MAX];

void setDepth(int v, int d) {
  if (v == NIL)
    return;
  D[v] = d;
  setDepth(A[v].l, d + 1);
  setDepth(A[v].r, d + 1);
}

int setHight(int v) {
  if (A[v].l == NIL) {
    return H[v] = 0;
  }
  int h1, h2;
  h1 = setHight(A[v].l) + 1;
  h2 = setHight(A[v].r) + 1;
  return H[v] = max(h1, h2);
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    A[i].p = A[i].l = A[i].r = NIL;
  }
  int v, r, l, root;
  for (int i = 0; i < n; i++) {
    cin >> v >> l >> r;
    if (l != NIL) {
      A[v].l = l;
      A[v].r = r;
      A[l].p = A[r].p = v;
    }
  }
  for (int i = 0; i < n; i++) {
    if (A[i].p == NIL)
      root = i;
  }
  setDepth(root, 0);
  setHight(root);
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << A[i].p << ", sibling = ";
    if (A[i].p == NIL)
      cout << "-1";
    else if (A[A[i].p].l != i)
      cout << A[A[i].p].l;
    else
      cout << A[A[i].p].r;
    cout << ", degree = ";
    if (A[i].l == NIL)
      cout << "0, ";
    else
      cout << "2, ";
    cout << "depth = " << D[i] << ", height = " << H[i] << ", ";
    if (D[i] == 0)
      cout << "root";
    else if (A[i].l == NIL)
      cout << "leaf";
    else
      cout << "internal node";
    cout << endl;
  }
}