Problem p02280 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

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

int n;
Node **nodes;

inline int getheight(int id) {
  Node *node = nodes[id];
  int left_height = 0, right_height = 0;
  if (node->left) {
    left_height = 1 + getheight(node->left->id);
  }
  if (node->right) {
    right_height = 1 + getheight(node->right->id);
  }
  return left_height > right_height ? left_height : right_height;
}

int main(void) {

  scanf("%d", &n);
  nodes = new Node *[n];
  for (int i = 0; i < n; ++i) {
    nodes[i] = NULL;
  }

  for (int i = 0, id, left, right; i < n; ++i) {
    scanf("%d %d %d", &id, &left, &right);
    Node *pnode = nodes[id];
    if (pnode == NULL) {
      pnode = nodes[id] = new Node;
      pnode->id = id;
      pnode->parent = pnode->left = pnode->right = NULL;
    }
    if (left != -1) {
      Node *leftnode = nodes[left];
      if (leftnode == NULL) {
        leftnode = nodes[left] = new Node;
        leftnode->id = left;
        leftnode->parent = leftnode->left = leftnode->right = NULL;
      }
      leftnode->parent = pnode;
      pnode->left = leftnode;
    }
    if (right != -1) {
      Node *rightnode = nodes[right];
      if (rightnode == NULL) {
        rightnode = nodes[right] = new Node;
        rightnode->id = right;
        rightnode->parent = rightnode->left = rightnode->right = NULL;
      }
      rightnode->parent = pnode;
      pnode->right = rightnode;
    }
  }

  for (int i = 0; i < n; ++i) {

    int parent = (nodes[i]->parent ? nodes[i]->parent->id : -1);

    int sibling = -1;
    if (parent != -1) {
      if (nodes[parent]->left && nodes[parent]->left != nodes[i]) {
        sibling = nodes[parent]->left->id;
      } else {
        sibling = nodes[parent]->right->id;
      }
    }

    int degree = 0;
    if (nodes[i]->left) {
      ++degree;
    }
    if (nodes[i]->right) {
      ++degree;
    }

    int depth = 0;
    Node *node = nodes[i]->parent;
    while (node) {
      ++depth;
      node = node->parent;
    }

    int height = getheight(i);

    const char *type = "internal node";
    if (nodes[i]->parent == NULL) {
      type = "root";
    } else if (!nodes[i]->left && !nodes[i]->right) {
      type = "leaf";
    }

    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d, %s\n",
           nodes[i]->id, parent, sibling, degree, depth, height, type);
  }

  for (int i = 0; i < n; ++i) {
    Node *node = nodes[i];
    node->parent = node->left = node->right = NULL;
    delete node;
  }

  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

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

int n;
Node **nodes;

inline int getheight(int id) {
  Node *node = nodes[id];
  int left_height = 0, right_height = 0;
  if (node->left) {
    left_height = 1 + getheight(node->left->id);
  }
  if (node->right) {
    right_height = 1 + getheight(node->right->id);
  }
  return left_height > right_height ? left_height : right_height;
}

int main(void) {

  scanf("%d", &n);
  nodes = new Node *[n];
  for (int i = 0; i < n; ++i) {
    nodes[i] = NULL;
  }

  for (int i = 0, id, left, right; i < n; ++i) {
    scanf("%d %d %d", &id, &left, &right);
    Node *pnode = nodes[id];
    if (pnode == NULL) {
      pnode = nodes[id] = new Node;
      pnode->id = id;
      pnode->parent = pnode->left = pnode->right = NULL;
    }
    if (left != -1) {
      Node *leftnode = nodes[left];
      if (leftnode == NULL) {
        leftnode = nodes[left] = new Node;
        leftnode->id = left;
        leftnode->parent = leftnode->left = leftnode->right = NULL;
      }
      leftnode->parent = pnode;
      pnode->left = leftnode;
    }
    if (right != -1) {
      Node *rightnode = nodes[right];
      if (rightnode == NULL) {
        rightnode = nodes[right] = new Node;
        rightnode->id = right;
        rightnode->parent = rightnode->left = rightnode->right = NULL;
      }
      rightnode->parent = pnode;
      pnode->right = rightnode;
    }
  }

  for (int i = 0; i < n; ++i) {

    int parent = (nodes[i]->parent ? nodes[i]->parent->id : -1);

    int sibling = -1;
    if (parent != -1) {
      Node *pnode = nodes[parent];
      if (pnode->left && pnode->left != nodes[i]) {
        sibling = pnode->left->id;
      } else if (pnode->right && pnode->right != nodes[i]) {
        sibling = pnode->right->id;
      }
    }

    int degree = 0;
    if (nodes[i]->left) {
      ++degree;
    }
    if (nodes[i]->right) {
      ++degree;
    }

    int depth = 0;
    Node *node = nodes[i]->parent;
    while (node) {
      ++depth;
      node = node->parent;
    }

    int height = getheight(i);

    const char *type = "internal node";
    if (nodes[i]->parent == NULL) {
      type = "root";
    } else if (!nodes[i]->left && !nodes[i]->right) {
      type = "leaf";
    }

    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d, %s\n",
           nodes[i]->id, parent, sibling, degree, depth, height, type);
  }

  for (int i = 0; i < n; ++i) {
    Node *node = nodes[i];
    node->parent = node->left = node->right = NULL;
    delete node;
  }

  return 0;
}