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

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

struct node {
  int parent;
  int degree;
  int sibling; // anh em
  int depth;
  int height; // li??n k???t
  vector<int> child;
};

int setheight(vector<node> tree, int id) {
  int l = 0, r = 0;
  if (tree[id].degree >= 1) {
    l = setheight(tree, tree[id].child[0]) + 1;
  }
  if (tree[id].degree >= 2) {
    r = setheight(tree, tree[id].child[1]) + 1;
  }
  return tree[id].height = max(l, r);
}

int main() {
  int n;
  cin >> n;
  vector<node> tree;
  tree.resize(n);
  int id, child1, child2;
  for (int i = 0; i < n; i++) {
    tree[i].depth = 0;
    tree[i].sibling = -1;
    tree[i].parent = -1;
  }
  for (int i = 0; i < n; i++) {
    cin >> id >> child1 >> child2;

    if (child1 != -1 && child2 != -1) {
      tree[id].child.resize(2);
      tree[id].degree = 2;
      tree[child1].sibling = child2;
      tree[child2].sibling = child1;
      tree[id].child[0] = child1;
      tree[id].child[1] = child2;

    }

    else if (child1 != -1 && child2 == -1) {
      tree[id].child.resize(1);
      tree[id].degree = 1;
      tree[id].child[0] = child1;
    } else if (child2 != -1 && child1 == -1) {
      tree[id].child.resize(1);
      tree[id].degree = 1;
      tree[id].child[0] = child2;
    } else
      tree[id].degree = 0;
  }
  for (int i = 0; i < n; i++) {
    tree[i].height = setheight(tree, i);
    if (tree[i].degree != 0) {
      for (int j = 0; j < tree[i].degree; j++) {
        tree[tree[i].child[j]].parent = i;
      }
    }
  }
  for (int i = 0; i < n; i++) {

    int j = i;
    while (tree[j].parent != -1) {
      j = tree[j].parent;
      ++tree[i].depth;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": "
         << "parent = " << tree[i].parent << ", sibling = " << tree[i].sibling
         << ", degree = " << tree[i].degree << ", depth = " << tree[i].depth
         << ", height = " << tree[i].height << ", ";
    if (tree[i].parent == -1)
      cout << "root" << endl;
    else if (tree[i].degree == 0)
      cout << "leaf" << endl;
    else
      cout << "internal node" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

struct node {
  int parent;
  int degree;
  int sibling; // anh em
  int depth;
  int height; // li??n k???t
  vector<int> child;
};

int setheight(vector<node> tree, int id) {
  int l = 0, r = 0;
  if (tree[id].degree >= 1) {
    l = setheight(tree, tree[id].child[0]) + 1;
  }
  if (tree[id].degree >= 2) {
    r = setheight(tree, tree[id].child[1]) + 1;
  }
  return tree[id].height = max(l, r);
}

int main() {
  int n;
  cin >> n;
  vector<node> tree;
  tree.resize(n);
  int id, child1, child2;
  for (int i = 0; i < n; i++) {
    tree[i].depth = 0;
    tree[i].sibling = -1;
    tree[i].parent = -1;
  }
  for (int i = 0; i < n; i++) {
    cin >> id >> child1 >> child2;

    if (child1 != -1 && child2 != -1) {
      tree[id].child.resize(2);
      tree[id].degree = 2;
      tree[child1].sibling = child2;
      tree[child2].sibling = child1;
      tree[id].child[0] = child1;
      tree[id].child[1] = child2;

    }

    else if (child1 != -1 && child2 == -1) {
      tree[i].child.resize(1);
      tree[id].degree = 1;
      tree[id].child[0] = child1;
    } else if (child2 != -1 && child1 == -1) {
      tree[i].child.resize(1);
      tree[id].degree = 1;
      tree[id].child[0] = child2;
    } else
      tree[id].degree = 0;
  }
  for (int i = 0; i < n; i++) {
    tree[i].height = setheight(tree, i);
    if (tree[i].degree != 0) {
      for (int j = 0; j < tree[i].degree; j++) {
        tree[tree[i].child[j]].parent = i;
      }
    }
  }
  for (int i = 0; i < n; i++) {

    int j = i;
    while (tree[j].parent != -1) {
      j = tree[j].parent;
      ++tree[i].depth;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": "
         << "parent = " << tree[i].parent << ", sibling = " << tree[i].sibling
         << ", degree = " << tree[i].degree << ", depth = " << tree[i].depth
         << ", height = " << tree[i].height << ", ";
    if (tree[i].parent == -1)
      cout << "root" << endl;
    else if (tree[i].degree == 0)
      cout << "leaf" << endl;
    else
      cout << "internal node" << endl;
  }
  return 0;
}