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

#include <iostream>
#include <stdlib.h>

using namespace std;

#define MAX_NODE 25

int n;

typedef struct {
  int num;
  int left;
  int right;
  int degree;
} binary_tree;

int getSibling(binary_tree A[], int parent, int key) {

  if (parent == -1) {
    return -1;
  } else if (A[parent].right == key) {
    return A[parent].left;
  } else if (A[parent].left == key) {
    return A[parent].right;
  }
}

int getDepth(int parent_list[], int key, int default_depth) {

  int depth = default_depth;

  if (parent_list[key] != -1) {
    depth = getDepth(parent_list, parent_list[key], depth + 1);
  }
  return depth;
}

int getHeight(binary_tree A[], int num, int default_height) {

  int height;
  int height_right = 0;
  int height_left = 0;

  if (A[num].right == -1 && A[num].left == -1) {
    return default_height;
  } else {
    if (A[num].right != -1) {
      height_right = getHeight(A, A[num].right, default_height + 1);
    }
    if (A[num].left != -1) {
      height_left = getHeight(A, A[num].left, default_height + 1);
    }
    if (height_right < height_left) {
      height = height_left;
    } else {
      height = height_right;
    }
  }
  return height;
}

char *getState(binary_tree A, int parent_list[]) {

  if (parent_list[A.num] == -1) {
    return "root";
  } else if (A.right == -1 && A.left == -1) {
    return "leaf";
  }
  return "internal node";
}

void print(binary_tree A[], int parent_list[]) {

  int i;

  for (i = 0; i < n; i++) {
    cout << "node " << A[i].num << ": ";
    cout << "parent = " << parent_list[A[i].num] << ", ";
    cout << "sibling = " << getSibling(A, parent_list[A[i].num], A[i].num)
         << ", ";
    cout << "degree = " << A[i].degree << ", ";
    cout << "depth = " << getDepth(parent_list, A[i].num, 0) << ", ";
    cout << "height = " << getHeight(A, A[i].num, 0) << ", ";
    cout << getState(A[i], parent_list);
    cout << endl;
  }
}

int main() {

  int i;
  int num;
  int parent[MAX_NODE];
  binary_tree *A;

  cin >> n;

  A = (binary_tree *)malloc(sizeof(binary_tree) * n);

  for (i = 0; i < MAX_NODE; i++) {
    parent[i] = -1;
  }

  for (i = 0; i < n; i++) {
    cin >> num;
    A[num].num = num;
    cin >> A[num].left;
    cin >> A[num].right;
    parent[A[num].left] = num;
    parent[A[num].right] = num;
    A[num].degree = 0;
    if (A[num].left >= 0) {
      A[num].degree++;
    }
    if (A[num].right >= 0) {
      A[num].degree++;
    }
  }

  print(A, parent);

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <stdlib.h>

using namespace std;

#define MAX_NODE 25

int n;

typedef struct {
  int num;
  int left;
  int right;
  int degree;
} binary_tree;

int getSibling(binary_tree A[], int parent, int key) {

  if (parent == -1) {
    return -1;
  } else if (A[parent].right == key) {
    return A[parent].left;
  } else if (A[parent].left == key) {
    return A[parent].right;
  }
}

int getDepth(int parent_list[], int key, int default_depth) {

  int depth = default_depth;

  if (parent_list[key] != -1) {
    depth = getDepth(parent_list, parent_list[key], depth + 1);
  }
  return depth;
}

int getHeight(binary_tree A[], int num, int default_height) {

  int height;
  int height_right;
  int height_left;

  if (A[num].right == -1 && A[num].left == -1) {
    return default_height;
  } else {
    height_right = getHeight(A, A[num].right, default_height + 1);
    height_left = getHeight(A, A[num].left, default_height + 1);
    if (height_right < height_left) {
      height = height_left;
    } else {
      height = height_right;
    }
  }
  return height;
}

char *getState(binary_tree A, int parent_list[]) {

  if (parent_list[A.num] == -1) {
    return "root";
  } else if (A.right == -1 && A.left == -1) {
    return "leaf";
  }
  return "internal node";
}

void print(binary_tree A[], int parent_list[]) {

  int i;

  for (i = 0; i < n; i++) {
    cout << "node " << A[i].num << ": ";
    cout << "parent = " << parent_list[A[i].num] << ", ";
    cout << "sibling = " << getSibling(A, parent_list[A[i].num], A[i].num)
         << ", ";
    cout << "degree = " << A[i].degree << ", ";
    cout << "depth = " << getDepth(parent_list, A[i].num, 0) << ", ";
    cout << "height = " << getHeight(A, A[i].num, 0) << ", ";
    cout << getState(A[i], parent_list);
    cout << endl;
  }
}

int main() {

  int i;
  int num;
  int parent[MAX_NODE];
  binary_tree *A;

  cin >> n;

  A = (binary_tree *)malloc(sizeof(binary_tree) * n);

  for (i = 0; i < MAX_NODE; i++) {
    parent[i] = -1;
  }

  for (i = 0; i < n; i++) {
    cin >> num;
    A[num].num = num;
    cin >> A[num].left;
    cin >> A[num].right;
    parent[A[num].left] = num;
    parent[A[num].right] = num;
    A[num].degree = 0;
    if (A[num].left >= 0) {
      A[num].degree++;
    }
    if (A[num].right >= 0) {
      A[num].degree++;
    }
  }

  print(A, parent);

  return 0;
}