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

#include <cstdio>
#define N 26
#define Nil -1

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

Node BT[N];
int D[N];
int Deg[N];
int H[N];

void setDepth(int i, int depth) {
  D[i] = depth;
  if (BT[i].left != Nil)
    setDepth(BT[i].left, depth + 1);
  if (BT[i].right != Nil)
    setDepth(BT[i].right, depth + 1);
}

int setHeight(int i) {
  int hl, hr;
  hl = hr = 0;
  if (BT[i].left != Nil)
    hl = setHeight(BT[i].left) + 1;
  if (BT[i].right != Nil)
    hr = setHeight(BT[i].right) + 1;
  if (hl < hr) {
    return hr;
  } else {
    return hl;
  }
}

int sibling(int i) {
  if (BT[i].parent == Nil)
    return -1;
  Node parent = BT[BT[i].parent];
  if (parent.left != Nil && parent.left != i) {
    return parent.left;
  } else if (parent.right != Nil && parent.right != i) {
    return parent.right;
  } else {
    return -1;
  }
}

int setDegree(int i) {
  int c = 0;
  if (BT[i].left != Nil)
    c++;
  if (BT[i].right != Nil)
    c++;
  return c;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    D[i] = -1;
  for (int i = 0; i < n; i++) {
    BT[i].parent = Nil;
  }

  for (int i = 0; i < n; i++) {
    int v, l, r;
    scanf("%d %d %d", &v, &l, &r);
    BT[v].left = l;
    BT[v].right = r;
    if (Nil != l) {
      BT[l].parent = v;
    }
    if (Nil != r) {
      BT[r].parent = v;
    }
  }

  int root = -1;
  for (int i = 0; i < n; i++)
    if (BT[i].parent == Nil)
      root = i;
  setDepth(root, 0);
  for (int i = 0; i < n; i++) {
    H[i] = setHeight(i);
    Deg[i] = setDegree(i);
  }
  for (int i = 0; i < n; i++) {
    char type[20];
    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d, ",
           i, BT[i].parent, sibling(i), Deg[i], D[i], H[i]);
    if (root == i) {
      printf("root\\n");
    } else if (Deg[i] > 0) {
      printf("internal node\\n");
    } else {
      printf("leaf\\n");
    }
  }

  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#define N 26
#define Nil -1

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

Node BT[N];
int D[N];
int Deg[N];
int H[N];

void setDepth(int i, int depth) {
  D[i] = depth;
  if (BT[i].left != Nil)
    setDepth(BT[i].left, depth + 1);
  if (BT[i].right != Nil)
    setDepth(BT[i].right, depth + 1);
}

int setHeight(int i) {
  int hl, hr;
  hl = hr = 0;
  if (BT[i].left != Nil)
    hl = setHeight(BT[i].left) + 1;
  if (BT[i].right != Nil)
    hr = setHeight(BT[i].right) + 1;
  if (hl < hr) {
    return hr;
  } else {
    return hl;
  }
}

int sibling(int i) {
  if (BT[i].parent == Nil)
    return -1;
  Node parent = BT[BT[i].parent];
  if (parent.left != Nil && parent.left != i) {
    return parent.left;
  } else if (parent.right != Nil && parent.right != i) {
    return parent.right;
  } else {
    return -1;
  }
}

int setDegree(int i) {
  int c = 0;
  if (BT[i].left != Nil)
    c++;
  if (BT[i].right != Nil)
    c++;
  return c;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    D[i] = -1;
  for (int i = 0; i < n; i++) {
    BT[i].parent = Nil;
  }

  for (int i = 0; i < n; i++) {
    int v, l, r;
    scanf("%d %d %d", &v, &l, &r);
    BT[i].left = l;
    BT[i].right = r;
    if (Nil != l) {
      BT[l].parent = v;
    }
    if (Nil != r) {
      BT[r].parent = v;
    }
  }

  int root = -1;
  for (int i = 0; i < n; i++)
    if (BT[i].parent == Nil)
      root = i;
  setDepth(root, 0);
  for (int i = 0; i < n; i++) {
    H[i] = setHeight(i);
    Deg[i] = setDegree(i);
  }
  for (int i = 0; i < n; i++) {
    char type[20];
    printf("node id: %d, parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d, ",
           i, BT[i].parent, sibling(i), Deg[i], D[i], H[i]);
    if (root == i) {
      printf("root\\n");
    } else if (Deg[i] > 0) {
      printf("internal node\\n");
    } else {
      printf("leaf\\n");
    }
  }

  return 0;
}