Problem p02280 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <vector>

using namespace std;

struct Node {
  Node() {
    brother = parent = -1;
    height = depth = num_of_children = 0;
  }
  int parent, brother, depth, num_of_children, height;
  vector<int> children;
};

void calcDepth(Node nodes[], int root_id) {
  for (int i = 0; i < nodes[root_id].num_of_children; i++) {
    nodes[nodes[root_id].children[i]].depth = nodes[root_id].depth + 1;
    calcDepth(nodes, nodes[root_id].children[i]);
  }
}

void calcHeight(Node nodes[], int height, int parent) {
  nodes[parent].height = max(nodes[parent].height, height + 1);
  if (nodes[parent].parent != -1)
    calcHeight(nodes, height + 1, nodes[parent].parent);
}

int main() {
  int n, id, left_id, right_id, root_id;
  scanf("%d", &n);
  Node nodes[n];

  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &id, &left_id, &right_id);
    if (left_id != -1) {
      nodes[id].children.push_back(left_id);
      nodes[id].num_of_children++;
      nodes[left_id].parent = id;
      if (right_id != -1) {
        nodes[id].children.push_back(right_id);
        nodes[id].num_of_children++;
        nodes[right_id].parent = id;
        nodes[left_id].brother = right_id;
        nodes[right_id].brother = left_id;
      }
    }
  }

  for (root_id = 0; root_id < n; root_id++) {
    if (nodes[root_id].parent == -1)
      break;
  }

  if (n > 1) {
    calcDepth(nodes, root_id);

    for (int i = 0; i < n; i++) {
      if (nodes[i].num_of_children == 0)
        calcHeight(nodes, 0, nodes[i].parent);
    }
  }

  for (int i = 0; i < n; i++) {
    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d,",
           i, nodes[i].parent, nodes[i].brother, nodes[i].num_of_children,
           nodes[i].depth, nodes[i].height);
    if (nodes[i].parent == -1) {
      printf(" root\n");
    } else if (nodes[i].num_of_children == 0) {
      printf(" leaf\n");
    } else {
      printf(" internal node\n");
    }
  }
}

Fixed submission:
#include <stdio.h>
#include <vector>

using namespace std;

struct Node {
  Node() {
    brother = parent = -1;
    height = depth = num_of_children = 0;
  }
  int parent, brother, depth, num_of_children, height;
  vector<int> children;
};

void calcDepth(Node nodes[], int root_id) {
  for (int i = 0; i < nodes[root_id].num_of_children; i++) {
    nodes[nodes[root_id].children[i]].depth = nodes[root_id].depth + 1;
    calcDepth(nodes, nodes[root_id].children[i]);
  }
}

void calcHeight(Node nodes[], int height, int parent) {
  nodes[parent].height = max(nodes[parent].height, height + 1);
  if (nodes[parent].parent != -1)
    calcHeight(nodes, height + 1, nodes[parent].parent);
}

int main() {
  int n, id, left_id, right_id, root_id;
  scanf("%d", &n);
  Node nodes[n];

  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &id, &left_id, &right_id);
    if (left_id != -1) {
      nodes[id].children.push_back(left_id);
      nodes[id].num_of_children++;
      nodes[left_id].parent = id;
    }
    if (right_id != -1) {
      nodes[id].children.push_back(right_id);
      nodes[id].num_of_children++;
      nodes[right_id].parent = id;
    }
    if (left_id != -1 && right_id != -1) {
      nodes[left_id].brother = right_id;
      nodes[right_id].brother = left_id;
    }
  }

  for (root_id = 0; root_id < n; root_id++) {
    if (nodes[root_id].parent == -1)
      break;
  }

  if (n > 1) {
    calcDepth(nodes, root_id);

    for (int i = 0; i < n; i++) {
      if (nodes[i].num_of_children == 0)
        calcHeight(nodes, 0, nodes[i].parent);
    }
  }

  for (int i = 0; i < n; i++) {
    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d,",
           i, nodes[i].parent, nodes[i].brother, nodes[i].num_of_children,
           nodes[i].depth, nodes[i].height);
    if (nodes[i].parent == -1) {
      printf(" root\n");
    } else if (nodes[i].num_of_children == 0) {
      printf(" leaf\n");
    } else {
      printf(" internal node\n");
    }
  }
}