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

#include <iostream>
#define NODE_MAX 100000
#define NIL -1
using namespace std;

struct Node {
  int parent_node = NIL;
  int leftChild_node = NIL;
  int rightSibling_node = NIL;
};

Node Tree[NODE_MAX];
int Depth_of_Node[NODE_MAX];

void CalcDepthByRecursion(int node_id, int depth) {
  Depth_of_Node[node_id] = depth;
  if (Tree[node_id].rightSibling_node != NIL) {
    CalcDepthByRecursion(Tree[node_id].rightSibling_node, depth);
  }
  if (Tree[node_id].leftChild_node != NIL) {
    CalcDepthByRecursion(Tree[node_id].leftChild_node, depth + 1);
  }
}

// Print out children nodes
void PrintChildrenNodes(int node_id) {
  printf("[");
  int child_node = Tree[node_id].leftChild_node, i = 0;
  while (child_node != NIL) {
    if (i)
      printf(", ");
    printf("%d", child_node);
    child_node = Tree[child_node].rightSibling_node;
    ++i;
  }
  printf("]");
}

int main() {
  int num_node, node_id, num_child, root;
  scanf("%d", &num_node);
  for (int i = 0; i < num_node; ++i) {
    scanf("%d %d", &node_id, &num_child);
    int child_node, prev_node;
    for (int j = 0; j < num_child; ++j) {
      scanf("%d", &child_node);
      if (!j)
        Tree[node_id].leftChild_node = child_node; // set left_child node
      Tree[child_node].parent_node = node_id;      // set parent_node
      if (j)
        Tree[prev_node].rightSibling_node =
            child_node;       // set rightSibling of previous node
      prev_node = child_node; // set current node as prev node
    }
  }

  // search node
  for (int i = 0; i < num_node; ++i) {
    if (Tree[i].parent_node == NIL) {
      root = i;
      break;
    }
  }

  CalcDepthByRecursion(root, 0);
  for (int i = 0; i < num_node; ++i) {
    printf("node %d: parent = %d, depth = %d, ", i, Tree[i].parent_node,
           Depth_of_Node[i]);
    if (Tree[i].parent_node == NIL)
      printf("root, ");
    else if (Tree[i].leftChild_node == NIL)
      printf("leaf, ");
    else
      printf("internal node, ");
    PrintChildrenNodes(i);
    printf("\\n");
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <math.h>
#include <stdio.h>

#define MAX 100
#define NIL -1

struct Node {
  int parent, left, right;
};

Node T[MAX];
int n, D[MAX];

// i??????root????????????????????§??\\??????
void setD(int i, int p) {
  D[i] = p;
  if (T[i].right != NIL)
    setD(T[i].right, p);
  if (T[i].left != NIL)
    setD(T[i].left, p + 1);
}

void print() {
  for (int i = 0; i < n; i++) {
    printf("node %d: ", i);
    printf("parent = %d, ", T[i].parent);
    printf("depth = %d, ", D[i]);
    if (T[i].parent == NIL) {
      printf("root, ");
      if (T[i].left == NIL) {
        printf("[]\\n");
        continue;
      }
    } else {
      if (T[i].left == NIL) {
        printf("leaf, []\\n");
        continue;
      } else
        printf("internal node, ");
    }
    //        childrenPrint(i);
    int c, j;
    printf("[");
    for (j = 0, c = T[i].left; c != NIL; j++, c = T[c].right) {
      if (j)
        printf(", ");
      printf("%d", c);
    }
    printf("]\\n");
  }
}

int main() {
  int id;
  int left;
  int ch_n;
  int root = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    T[i].parent = T[i].left = T[i].right = NIL;
  }

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &id, &ch_n);
    for (int j = 0; j < ch_n; j++) {
      if (j == 0) {
        scanf("%d", &T[id].left);
        T[T[id].left].parent = id;
        left = T[id].left;
      } else {
        scanf("%d", &T[left].right);
        T[T[left].right].parent = id;
        left = T[left].right;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (T[i].parent == NIL)
      root = i;
  }

  setD(root, 0);

  print();

  return 0;
}