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

#include "iostream"
#include "stdio.h"
#include "vector"
using namespace std;
class branch {
public:
  int id;
  int height;
  int sibling;
  int degree;
  int depth;
  int p;
  int right;
  int left;
  void print() {
    cout << "node " << id << ": parent = " << p << ", sibling = " << sibling
         << ", degree = " << degree << ", depth = " << depth
         << ", height = " << height << ", ";
    if (p == -1) {
      cout << "root";
    } else if (right == -1 && left == -1) {
      cout << "leaf";
    } else {
      cout << "internal node";
    }
    cout << endl;
  }
};
vector<branch> tree;
int countDepth(int id) {
  if (tree[id].p != -1)
    return 1 + countDepth(tree[id].p);
  return 0;
}
int countHeight(int id) {
  int right, left;
  if (tree[id].right != -1 && tree[id].left != -1) {
    right = 1 + countHeight(tree[id].right);
    left = 1 + countHeight(tree[id].left);
    if (right < left)
      return left;
    else
      return right;
  } else if (tree[id].right != -1) {
    return 1 + countHeight(tree[id].right);
  } else if (tree[id].left != -1) {
    return 1 + countHeight(tree[id].left);
  }
  return 0;
}

int main() {
  branch buf;
  buf.p = -1;
  buf.sibling = -1;
  buf.degree = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    tree.push_back(buf);
  }
  for (int i = 0; i < n; i++) {
    int id;
    int right, left;
    cin >> id;
    tree[id].id = id;
    cin >> right;
    tree[id].right = right;
    cin >> left;
    tree[id].left = left;
    if (right != -1) {
      tree[id].degree++;
      tree[right].p = id;
      tree[right].sibling = left;
    }
    if (left != -1) {
      tree[id].degree++;
      tree[left].p = id;
      tree[left].sibling = right;
    }
  }
  for (int i = 0; i < n; i++) {
    tree[i].depth = countDepth(tree[i].id);
    tree[i].height = countHeight(tree[i].id);
    tree[i].print();
  }
  return 0;
}


Error: 0

Fixed submission:

#include "iostream"
#include "stdio.h"
#include "vector"
using namespace std;
class branch {
public:
  int id;
  int height;
  int sibling;
  int degree;
  int depth;
  int p;
  int right;
  int left;
  void print() {
    cout << "node " << id << ": parent = " << p << ", sibling = " << sibling
         << ", degree = " << degree << ", depth = " << depth
         << ", height = " << height << ", ";
    if (p == -1) {
      cout << "root";
    } else if (right == -1 && left == -1) {
      cout << "leaf";
    } else {
      cout << "internal node";
    }
    cout << endl;
  }
};
vector<branch> tree;
int countDepth(int id) {
  if (tree[id].p != -1)
    return 1 + countDepth(tree[id].p);
  return 0;
}
int countHeight(int id) {
  int right, left;
  if (tree[id].right != -1 && tree[id].left != -1) {
    right = 1 + countHeight(tree[id].right);
    left = 1 + countHeight(tree[id].left);
    if (right < left)
      return left;
    else
      return right;
  } else if (tree[id].right != -1) {
    return 1 + countHeight(tree[id].right);
  } else if (tree[id].left != -1) {
    return 1 + countHeight(tree[id].left);
  }
  return 0;
}

int main() {
  branch buf;
  buf.p = -1;
  buf.sibling = -1;
  buf.degree = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    tree.push_back(buf);
  }
  for (int i = 0; i < n; i++) {
    int id;
    int right, left;
    cin >> id;
    tree[id].id = id;
    cin >> right;
    tree[id].right = right;
    tree[right].p = id;
    cin >> left;
    tree[id].left = left;
    tree[left].p = id;
    tree[right].sibling = left;
    tree[left].sibling = right;
    if (right != -1) {
      tree[right].p = id;
      tree[right].sibling = left;
    }
    if (left != -1) {
      tree[left].p = id;
      tree[left].sibling = right;
    }
    if (right != -1) {
      tree[id].degree++;
      if (left != -1) {
        tree[id].degree++;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    tree[i].depth = countDepth(tree[i].id);
    tree[i].height = countHeight(tree[i].id);
    tree[i].print();
  }
  return 0;
}