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

#include <cstdio>
#include <iostream>
using namespace std;

const int nil = -1;
struct node {
  int parent;
  int degree;
  int left;
  int right;
  int sibling;
  int depth;
  int height;
};
node *nodes;
int n;

int height_setd(int id,
                int d) //????????????height, ????????¨??¨??????depth????¨????
{
  // if( d==0 )
  //	cout<<"root : "<<id<<endl;
  int res;
  if (id == nil) {
    res = -1;
  } else {
    nodes[id].depth = d;
    res = nodes[id].height = max(1 + height_setd(nodes[id].left, d + 1),
                                 1 + height_setd(nodes[id].right, d + 1));
  }
  return res;
}
int trackRoot(int start) {
  int prt = nodes[start].parent;
  if (prt == nil)
    return start;
  else
    return trackRoot(prt);
}

void show() {
  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].sibling, nodes[i].degree,
           nodes[i].depth, nodes[i].height);

    if (nodes[i].parent == nil) {
      printf("root\\n");
    } else if (nodes[i].degree == 0) {
      printf("leaf\\n");
    } else {
      printf("internal node\\n");
    }
  }
}

int main() {
  cin >> n;
  nodes = new node[n];
  for (int i = 0; i < n; i++)
    nodes[i].parent = nodes[i].left = nodes[i].right = nodes[i].sibling = nil;

  for (int i = 0, id, l, r; i < n; i++) {
    cin >> id;

    cin >> l >> r;
    if (l != nil) {
      nodes[id].left = l;
      nodes[l].parent = id;
    }
    if (r != nil) {
      nodes[id].right = r;
      nodes[r].parent = id;
    }
    if (r != nil && l != nil) {
      nodes[id].degree = 2;
      nodes[r].sibling = l;
      nodes[l].sibling = r;
    } else if (r == nil && l == nil) {
      nodes[id].degree = 0;
    } else {
      nodes[id].degree = 1;
    }
    l = r = nil;
  }
  // cout<<"h(root) :"<<height_setd(root, 0)<<endl;;
  height_setd(trackRoot(0), 0);
  show();
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

const int nil = -1;
struct node {
  int parent;
  int degree;
  int left;
  int right;
  int sibling;
  int depth;
  int height;
};
node *nodes;
int n;

int height_setd(int id,
                int d) //????????????height, ????????¨??¨??????depth????¨????
{
  // if( d==0 )
  //	cout<<"root : "<<id<<endl;

  nodes[id].depth = d;
  if (nodes[id].degree == 0) {
    return 0;
  } else {
    nodes[id].height = max(1 + height_setd(nodes[id].left, d + 1),
                           1 + height_setd(nodes[id].right, d + 1));
  }
}
int trackRoot(int start) {
  int prt = nodes[start].parent;
  if (prt == nil)
    return start;
  else
    return trackRoot(prt);
}

void show() {
  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].sibling, nodes[i].degree,
           nodes[i].depth, nodes[i].height);

    if (nodes[i].parent == nil) {
      printf("root\\n");
    } else if (nodes[i].degree == 0) {
      printf("leaf\\n");
    } else {
      printf("internal node\\n");
    }
  }
}

int main() {
  cin >> n;
  nodes = new node[n];
  for (int i = 0; i < n; i++)
    nodes[i].parent = nodes[i].left = nodes[i].right = nodes[i].sibling = nil;

  for (int i = 0, id, l, r; i < n; i++) {
    cin >> id;

    cin >> l >> r;
    if (l != nil) {
      nodes[id].left = l;
      nodes[l].parent = id;
    }
    if (r != nil) {
      nodes[id].right = r;
      nodes[r].parent = id;
    }
    if (r != nil && l != nil) {
      nodes[id].degree = 2;
      nodes[r].sibling = l;
      nodes[l].sibling = r;
    } else if (r == nil && l == nil) {
      nodes[id].degree = 0;
    } else {
      nodes[id].degree = 1;
    }
    l = r = nil;
  }
  // cout<<"h(root) :"<<height_setd(root, 0)<<endl;;
  height_setd(trackRoot(0), 0);
  show();
}