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

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

#define MAX 26
#define NIL -1

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

Node T[MAX];
int n, H[MAX];
int ch_num[MAX];

int setDepth(int id) {
  int d = 0;
  Node tmp = T[id];
  while (tmp.parent != NIL) {
    tmp = T[tmp.parent];
    d++;
  }
  return d;
}

int setHeight(int H[], int id) {
  int h1 = 0, h2 = 0;
  if (T[id].right != NIL) {
    h1 = setHeight(H, T[id].right) + 1;
  }
  if (T[id].left != NIL) {
    h2 = setHeight(H, T[id].left) + 1;
  }
  return H[id] = std::max(h1, h2);
}

int getsibling(int id) {
  if (T[id].parent == NIL)
    return -1;

  if (T[T[id].parent].left == id) {
    return T[T[id].parent].right;
  } else {
    return T[T[id].parent].left;
  }
}

void print(int ch_num[]) {
  for (int i = 0; i < n; i++) {
    printf("node %d: ", i);
    printf("parent = %d, ", T[i].parent);
    int sibling = getsibling(i);
    printf("sibling = %d, ", sibling);
    printf("degree = %d, ", ch_num[i]);
    printf("depth = %d, ", setDepth(i));
    printf("height = %d, ", setHeight(H, i));
    if (setDepth(i) == 0)
      printf("root\\n");
    else if (setHeight(H, i) != 0)
      printf("internal node\\n");
    else
      printf("leaf\\n");
  }
}

int main() {
  int id;
  //    int *ch_num = new int();
  scanf("%d", &n);

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

  for (int i = 0; i < n; i++) {
    scanf("%d", &id);
    scanf("%d %d", &T[id].left, &T[id].right);
    ch_num[id] = 0;
    // set the parent id
    if (T[id].left != NIL) {
      T[T[id].left].parent = id;
      ch_num[id]++;
    }
    if (T[id].right != NIL) {
      T[T[id].right].parent = id;
      ch_num[id]++;
    }
  }

  print(ch_num);

  return 0;
}

Error: 0

Fixed submission:

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

#define MAX 26
#define NIL -1

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

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

int setDepth(int id) {
  int d = 0;
  Node tmp = T[id];
  while (tmp.parent != NIL) {
    tmp = T[tmp.parent];
    d++;
  }
  return d;
}

int setHeight(int H[], int id) {
  int h1 = 0, h2 = 0;
  if (T[id].right != NIL) {
    h1 = setHeight(H, T[id].right) + 1;
  }
  if (T[id].left != NIL) {
    h2 = setHeight(H, T[id].left) + 1;
  }
  return H[id] = std::max(h1, h2);
}

int getsibling(int id) {
  if (T[id].parent == NIL)
    return -1;

  if (T[T[id].parent].left == id) {
    return T[T[id].parent].right;
  } else {
    return T[T[id].parent].left;
  }
}

void print(int *ch_num) {
  for (int i = 0; i < n; i++) {
    printf("node %d: ", i);
    printf("parent = %d, ", T[i].parent);
    int sibling = getsibling(i);
    printf("sibling = %d, ", sibling);
    printf("degree = %d, ", ch_num[i]);
    printf("depth = %d, ", setDepth(i));
    printf("height = %d, ", setHeight(H, i));
    if (setDepth(i) == 0)
      printf("root\\n");
    else if (setHeight(H, i) != 0)
      printf("internal node\\n");
    else
      printf("leaf\\n");
  }
}

int main() {
  int id;
  int *ch_num = new int();
  scanf("%d", &n);

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

  for (int i = 0; i < n; i++) {
    scanf("%d", &id);
    scanf("%d %d", &T[id].left, &T[id].right);
    ch_num[id] = 0;
    // set the parent id
    if (T[id].left != NIL) {
      T[T[id].left].parent = id;
      ch_num[id]++;
    }
    if (T[id].right != NIL) {
      T[T[id].right].parent = id;
      ch_num[id]++;
    }
  }

  print(ch_num);

  delete ch_num;
  return 0;
}