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

#include <algorithm>
#include <iostream>
#include <string>

struct tree {
  int id, left, right, parent, sibling, degree, depth, height;
  std::string type;
};

int make_height(tree trees[], int num) {
  if (trees[num].left == -1 && trees[num].right == -1) { // ???
    trees[num].height = 0;
    return 0;
  }
  if (trees[num].left == -1) {
    if (trees[num].right != -1) {
      trees[num].height = make_height(trees, trees[num].right) + 1;
    }
  } else if (trees[num].right == -1) {
    if (trees[num].left != -1) {
      trees[num].height = make_height(trees, trees[num].left) + 1;
    }
  } else {
    trees[num].height = std::max(make_height(trees, trees[num].left),
                                 make_height(trees, trees[num].right)) +
                        1;
  }
  return trees[num].height;
}

void make_node(tree trees[], int num, int depth) {
  if (trees[num].left != -1) {
    trees[num].type = "internal node";
    trees[num].depth = depth;
    make_node(trees, trees[num].left, depth + 1);
  }
  if (trees[num].right != -1) {
    trees[num].type = "internal node";
    trees[num].depth = depth;
    make_node(trees, trees[num].right, depth + 1);
  }
  if (trees[num].right == trees[num].left) {
    trees[num].type = "leaf";
    trees[num].depth = depth;
  }
}

void make_parent_sibling(tree trees[], int n) {
  for (int i = 0; i < n; i++) {
    trees[trees[i].left].parent = trees[i].id;     //???????¨????
    trees[trees[i].right].parent = trees[i].id;    //???????¨????
    trees[trees[i].left].sibling = trees[i].right; // ??????????¨????
    trees[trees[i].right].sibling = trees[i].left; // ??????????¨????
  }
}

void sort(tree trees[], int n) {
  tree temp;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < n - i; j++) {
      if (trees[j - 1].id > trees[j].id) {
        temp = trees[j - 1];
        trees[j - 1] = trees[j];
        trees[j] = temp;
      }
    }
  }
}

int main() {
  int n, id, left, right;
  tree trees[25];

  std::cin >> n;

  for (int i = 0; i < n; i++) {
    trees[i].parent = -1;
    trees[i].sibling = -1;
    trees[i].height = 0;
    trees[i].degree = 0;
    trees[i].depth = 0;
  }

  for (int i = 0; i < n; i++) {
    std::cin >> id >> left >> right;
    trees[i].id = id;
    trees[i].left = left;
    trees[i].right = right;
    if (right != -1 && left != -1) {
      trees[i].degree = 2; //????????°
    } else if (left == -1 && right == -1) {
      trees[i].degree = 0;
    } else {
      trees[i].degree = 1;
    }
  }

  sort(trees, n);
  make_parent_sibling(trees, n);

  for (int i = 0; i < n; i++) {
    if (trees[i].parent == -1) {
      make_height(trees, i);
      make_node(trees, i, 0);
      trees[i].type = "root";
      trees[i].depth = 0;
      break;
    }
  }

  for (int i = 0; i < n; i++) {
    std::cout << "node " << trees[i].id << ": ";
    std::cout << "parent = " << trees[i].parent << ", ";
    std::cout << "sibling = " << trees[i].sibling << ", ";
    std::cout << "degree = " << trees[i].degree << ", ";
    std::cout << "depth = " << trees[i].depth << ", ";
    std::cout << "height = " << trees[i].height << ", ";
    std::cout << trees[i].type;
    std::cout << std::endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

struct tree {
  int id, left, right, parent, sibling, degree, depth, height;
  std::string type;
};

int make_height(tree trees[], int num) {
  if (trees[num].left == -1 && trees[num].right == -1) {
    trees[num].height = 0;
    return 0;
  }
  trees[num].height = std::max(make_height(trees, trees[num].left),
                               make_height(trees, trees[num].right)) +
                      1;
  return trees[num].height;
}

void make_node(tree trees[], int num, int depth) {
  if (trees[num].left != -1) {
    trees[num].type = "internal node";
    trees[num].depth = depth;
    make_node(trees, trees[num].left, depth + 1);
  }
  if (trees[num].right != -1) {
    trees[num].type = "internal node";
    trees[num].depth = depth;
    make_node(trees, trees[num].right, depth + 1);
  }
  if (trees[num].right == trees[num].left) {
    trees[num].type = "leaf";
    trees[num].depth = depth;
  }
}

void make_parent_sibling(tree trees[], int n) {
  for (int i = 0; i < n; i++) {
    trees[trees[i].left].parent = trees[i].id;     //???????¨????
    trees[trees[i].right].parent = trees[i].id;    //???????¨????
    trees[trees[i].left].sibling = trees[i].right; // ??????????¨????
    trees[trees[i].right].sibling = trees[i].left; // ??????????¨????
  }
}

void sort(tree trees[], int n) {
  tree temp;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < n - i; j++) {
      if (trees[j - 1].id > trees[j].id) {
        temp = trees[j - 1];
        trees[j - 1] = trees[j];
        trees[j] = temp;
      }
    }
  }
}

int main() {
  int n, id, left, right;
  tree trees[25];

  std::cin >> n;

  for (int i = 0; i < n; i++) {
    trees[i].parent = -1;
    trees[i].sibling = -1;
    trees[i].height = 0;
    trees[i].degree = 0;
    trees[i].depth = 0;
  }

  for (int i = 0; i < n; i++) {
    std::cin >> id >> left >> right;
    trees[i].id = id;
    trees[i].left = left;
    trees[i].right = right;
    if (trees[i].right != -1 && trees[i].left != -1) {
      trees[i].degree = 2; //????????°
    } else {
      trees[i].degree = 0;
    }
  }

  sort(trees, n);
  make_parent_sibling(trees, n);

  for (int i = 0; i < n; i++) {
    if (trees[i].parent == -1) {
      make_height(trees, i);
      make_node(trees, i, 0);
      trees[i].type = "root";
      trees[i].depth = 0;
      break;
    }
  }

  for (int i = 0; i < n; i++) {
    std::cout << "node " << trees[i].id << ": ";
    std::cout << "parent = " << trees[i].parent << ", ";
    std::cout << "sibling = " << trees[i].sibling << ", ";
    std::cout << "degree = " << trees[i].degree << ", ";
    std::cout << "depth = " << trees[i].depth << ", ";
    std::cout << "height = " << trees[i].height << ", ";
    std::cout << trees[i].type;
    std::cout << std::endl;
  }

  return 0;
}