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

#include <bits/stdc++.h>
using namespace std;

typedef struct {

  int parent, bro, degree, depth, type, height;
  int r, l;

} node_t;

vector<node_t> tree;

int Depth(int num, int d) {

  int lowest = 0;
  int work = 0;

  tree[num].depth = d;

  if (tree[num].degree == 0) {
    tree[num].height = 0;
    return 1;
  }

  if (tree[num].l != -1)
    lowest = Depth(tree[num].l, d + 1);
  if (tree[num].r != -1)
    work = Depth(tree[num].r, d + 1);

  lowest = (lowest < work) ? work : lowest;
  tree[num].height = lowest;

  return lowest + 1;
}

int main() {

  int n;
  int num;

  cin >> n;
  tree.resize(n);
  for (int i = 0; i < n; i++) {
    tree[i].parent = tree[i].bro = -1;
    tree[i].degree = 0;
    tree[i].type = 2;
  }

  for (int i = 0; i < n; i++) {
    cin >> num;
    cin >> tree[num].l >> tree[num].r;
    if (tree[num].l != -1) {
      tree[num].type = 1;
      tree[tree[num].l].parent = num;
      tree[tree[num].l].bro = tree[num].r;
      tree[num].degree++;
    }
    if (tree[num].r != -1) {
      tree[num].type = 1;
      tree[tree[num].r].parent = num;
      tree[tree[num].r].bro = tree[num].l;
      tree[num].degree++;
    }
  }

  for (int i = 0; i < n; i++) {
    if (tree[i].parent == -1) {
      tree[i].type = 0;
      tree[i].depth = 0;
      Depth(i, 0);
      break;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << tree[i].parent
         << ", sibling = " << tree[i].bro << ", degree = " << tree[i].degree
         << ", depth = " << tree[i].depth << ", height = " << tree[i].height
         << ", ";

    if (tree[i].type == 0)
      cout << "root" << endl;
    else if (tree[i].type == 1)
      cout << "internal node" << endl;
    else
      cout << "leaf" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef struct {

  int parent, bro, degree, depth, type, height;
  int r, l;

} node_t;

vector<node_t> tree;

int Depth(int num, int d) {

  int lowest = 0;
  int work = 0;

  tree[num].depth = d;

  if (tree[num].degree == 0) {
    tree[num].height = 0;
    return 1;
  }

  if (tree[num].l != -1)
    lowest = Depth(tree[num].l, d + 1);
  if (tree[num].r != -1)
    work = Depth(tree[num].r, d + 1);

  lowest = (lowest < work) ? work : lowest;
  tree[num].height = lowest;

  return lowest + 1;
}

int main() {

  int n;
  int num;

  cin >> n;
  tree.resize(n);
  for (int i = 0; i < n; i++) {
    tree[i].parent = tree[i].bro = -1;
    tree[i].degree = tree[i].depth = tree[i].height = 0;
    tree[i].type = 2;
  }

  for (int i = 0; i < n; i++) {
    cin >> num;
    cin >> tree[num].l >> tree[num].r;
    if (tree[num].l != -1) {
      tree[num].type = 1;
      tree[tree[num].l].parent = tree[tree[num].r].parent = num;
      tree[tree[num].l].bro = tree[num].r;
      tree[tree[num].r].bro = tree[num].l;
    }
    if (tree[num].l != -1)
      tree[num].degree++;
    if (tree[num].r != -1)
      tree[num].degree++;
  }

  for (int i = 0; i < n; i++) {
    if (tree[i].parent == -1) {
      tree[i].type = 0;
      tree[i].depth = 0;
      Depth(i, 0);
      break;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << tree[i].parent
         << ", sibling = " << tree[i].bro << ", degree = " << tree[i].degree
         << ", depth = " << tree[i].depth << ", height = " << tree[i].height
         << ", ";

    if (tree[i].type == 0)
      cout << "root" << endl;
    else if (tree[i].type == 1)
      cout << "internal node" << endl;
    else
      cout << "leaf" << endl;
  }

  return 0;
}