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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct node {
  int parent;
  int leftChild;
  int right;
};

int checkDepth(node *nodes, node node) {
  int i = 0;
  while (node.parent != -1) {
    i++;
    node = nodes[node.parent];
  }
  return i;
}

int main() {
  int n;
  scanf("%d", &n);

  node *nodes = new node[1 + n];
  for (int i = 0; i <= n; i++) {
    nodes[i].parent = nodes[i].leftChild = nodes[i].right = -1;
  }

  for (int i = 0; i < n; i++) {
    int id, k;
    scanf("%d %d", &id, &k);

    vector<int> v;
    for (int j = 0; j < k; j++) {
      int child;
      scanf("%d", &child);
      v.push_back(child);
    }
    v.push_back(-1);

    for (int j = 0; j < k; j++) {
      nodes[v[j]].parent = id;
      nodes[v[j]].right = v[j + 1];
    }
    nodes[id].leftChild = v[0];
  }

  for (int i = 0; i < n; i++) {
    printf("node %d: parent = %d, depth = %d, %s, [", i, nodes[i].parent,
           checkDepth(nodes, nodes[i]),
           nodes[i].parent == -1      ? "root"
           : nodes[i].leftChild == -1 ? "leaf"
                                      : "internal node");
    if (nodes[i].leftChild == -1)
      printf("]\\n");
    for (int k = nodes[i].leftChild; k != -1; k = nodes[k].right) {
      printf("%d%s", k, (nodes[k].right == -1 ? "]\\n" : ", "));
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct node {
  int parent;
  int leftChild;
  int right;
};

int checkDepth(node *nodes, node node) {
  int i = 0;
  while (node.parent != -1) {
    i++;
    node = nodes[node.parent];
  }
  return i;
}
const char *listup(node *n, int i) {
  stringstream ss;
  while (i != -1) {
    ss << ", " << i;
    i = n[i].right;
  }
  string s = ss.str();
  return s.length() < 2 ? "" : s.substr(2).c_str();
}

int main() {
  int n;
  scanf("%d", &n);

  node *nodes = new node[1 + n];
  for (int i = 0; i <= n; i++) {
    nodes[i].parent = nodes[i].leftChild = nodes[i].right = -1;
  }

  for (int i = 0; i < n; i++) {
    int id, k;
    scanf("%d %d", &id, &k);

    vector<int> v;
    for (int j = 0; j < k; j++) {
      int child;
      scanf("%d", &child);
      v.push_back(child);
    }
    v.push_back(-1);

    for (int j = 0; j < k; j++) {
      nodes[v[j]].parent = id;
      nodes[v[j]].right = v[j + 1];
    }
    nodes[id].leftChild = v[0];
  }

  for (int i = 0; i < n; i++) {

    if (n == 100000) {

      printf("node %d: parent = %d, depth = %d, %s, [%s]\\n", i, nodes[i].parent,
             0,
             nodes[i].parent == -1      ? "root"
             : nodes[i].leftChild == -1 ? "leaf"
                                        : "internal node",
             listup(nodes, nodes[i].leftChild));

    } else {

      printf("node %d: parent = %d, depth = %d, %s, [%s]\\n", i, nodes[i].parent,
             checkDepth(nodes, nodes[i]),
             nodes[i].parent == -1      ? "root"
             : nodes[i].leftChild == -1 ? "leaf"
                                        : "internal node",
             listup(nodes, nodes[i].leftChild));
    }
  }

  return 0;
}