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

#include <bits/stdc++.h>

using namespace std;

typedef struct node_ {
  int id;
  int left;
  int right;
  int degree;
  int sib;
  int depth;
  int height;
  int parent;
} *node;

void printTree(vector<node> tree, int n) {
  for (int i = 0; i < n; i++) {
    node nd = tree[i];
    char *kind = new char[15];
    if (nd->parent == -1) {
      strcpy(kind, "root");
    } else if (nd->degree == 0) {
      strcpy(kind, "leaf");
    } else {
      strcpy(kind, "internal node");
    }
    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d, %s\\n",
           nd->id, nd->parent, nd->sib, nd->degree, nd->depth, nd->height,
           kind);
  }
}

int culHeight(vector<node> tree, node nd) {
  if (nd->degree == 0) {
    nd->height = 0;
    return 0;
  } else if (nd->degree == 2) {
    node l = tree[nd->left];
    node r = tree[nd->right];
    int lheight = culHeight(tree, l);
    int rheight = culHeight(tree, r);
    int h = max(lheight, rheight);
    nd->height = h + 1;
    return h + 1;
  } else if (nd->degree == 1) {
    int p = max(nd->left, nd->right);
    int h = culHeight(tree, tree[p]);
    nd->height = h + 1;
    return h + 1;
  }
  return -500;
}

int main() {
  int n;
  scanf("%d", &n);
  vector<node> tree(n);
  vector<int> find_root(n);
  for (int i = 0; i < n; i++) {
    node nd = new node_;
    tree[i] = nd;
    find_root[i] = 1;
    nd->parent = -1;
    nd->depth = -1;
    nd->height = -1;
    nd->degree = -1;
    nd->left = -1;
    nd->right = -1;
    nd->sib = -1;
  }
  for (int i = 0; i < n; i++) {
    int id, l, r;
    scanf("%d %d %d", &id, &l, &r);
    node nd = tree[id];
    nd->id = id;
    nd->left = l;
    nd->right = r;
    if (l == -1 && r == -1) {
      nd->degree = 0;
    } else if (l == -1 || r == -1) {
      int p = max(l, r);
      nd->degree = 1;
      tree[p]->parent = id;
      find_root[p] = 0;
    } else {
      nd->degree = 2;
      tree[l]->parent = id;
      tree[r]->parent = id;
      tree[l]->sib = r;
      tree[r]->sib = l;
      find_root[l] = 0;
      find_root[r] = 0;
    }
  } // tree自体はできた

  int rootid = 0;
  while (find_root[rootid] == 0) {
    rootid++;
  }
  node rootnd = tree[rootid];
  rootnd->depth = 0;

  queue<node> Q;
  Q.push(rootnd);
  while (Q.empty() == 0) {
    node nd = Q.front();
    Q.pop();
    if (nd->degree == 2) {
      Q.push(tree[nd->left]);
      tree[nd->left]->depth = nd->depth + 1;
      Q.push(tree[nd->right]);
      tree[nd->right]->depth = nd->depth + 1;
    } else if (nd->degree == 1) {
      int p = max(nd->left, nd->right);
      Q.push(tree[p]);
      tree[p]->depth = nd->depth + 1;
    }
  }

  int a = culHeight(tree, rootnd);
  printTree(tree, n);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef struct node_ {
  int id;
  int left;
  int right;
  int degree;
  int sib;
  int depth;
  int height;
  int parent;
} *node;

void printTree(vector<node> tree, int n) {
  for (int i = 0; i < n; i++) {
    node nd = tree[i];
    char *kind = new char[15];
    if (nd->parent == -1) {
      strcpy(kind, "root");
    } else if (nd->degree == 0) {
      strcpy(kind, "leaf");
    } else {
      strcpy(kind, "internal node");
    }
    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d, %s\\n",
           nd->id, nd->parent, nd->sib, nd->degree, nd->depth, nd->height,
           kind);
  }
}

int culHeight(vector<node> tree, node nd) {
  if (nd->degree == 0) {
    nd->height = 0;
    return 0;
  } else {
    node l = tree[nd->left];
    node r = tree[nd->right];
    int lheight = culHeight(tree, l);
    int rheight = culHeight(tree, r);
    int h = max(lheight, rheight);
    nd->height = h + 1;
    return h + 1;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  vector<node> tree(n);
  vector<int> find_root(n);
  for (int i = 0; i < n; i++) {
    node nd = new node_;
    tree[i] = nd;
    find_root[i] = 1;
    nd->parent = -1;
    nd->depth = -1;
    nd->height = -1;
    nd->degree = -1;
    nd->sib = -1;
  }
  for (int i = 0; i < n; i++) {
    int id, l, r;
    scanf("%d %d %d", &id, &l, &r);
    node nd = tree[id];
    nd->id = id;
    nd->left = l;
    nd->right = r;
    if (l == -1) {
      nd->degree = 0;
    } else {
      nd->degree = 2;
      tree[l]->parent = id;
      tree[r]->parent = id;
      tree[l]->sib = r;
      tree[r]->sib = l;
      find_root[l] = 0;
      find_root[r] = 0;
    }
  } // tree自体はできた

  int rootid = 0;
  while (find_root[rootid] == 0) {
    rootid++;
  }
  node rootnd = tree[rootid];
  rootnd->depth = 0;

  queue<node> Q;
  Q.push(rootnd);
  while (Q.empty() == 0) {
    node nd = Q.front();
    Q.pop();
    if (nd->degree == 2) {
      Q.push(tree[nd->left]);
      tree[nd->left]->depth = nd->depth + 1;
      Q.push(tree[nd->right]);
      tree[nd->right]->depth = nd->depth + 1;
    }
  }

  int a = culHeight(tree, rootnd);
  printTree(tree, n);
  return 0;
}