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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

int height_cnt;

struct node {
  int id;
  // int depth;
  // int height;
  // int degree;
  string type;
  int parent;
  int left;
  int right;
  int sibling;
};

node p[25];

int depth_search(int i) {
  int cnt = 0;

  while (p[i].parent != -1) {
    cnt++;
    i = p[i].parent;
  }

  return cnt;
}

int height_search(int i) {
  int hr, hl;
  hr = 0;
  hl = 0;

  if (p[i].left != -1) {
    hl = height_search(p[i].left) + 1;
  }
  if (p[i].right != -1) {
    hr = height_search(p[i].right) + 1;
  }

  return max(hr, hl);
}

int parent_search(int i) {
  if (p[i].parent == -1) {
    p[i].type = "root";
    return -1;
  } else {
    return p[i].parent;
  }
}

int degree_search(int i) {
  if (p[i].left != -1 && p[i].right != -1) {
    return 2;
  } else if (p[i].left != -1 || p[i].right != -1) {
    return 1;
  } else {
    p[i].type = "leaf";
    return 0;
  }
}

int sibling_id(int i) {
  if (p[i].sibling == -1) {
    return -1;
  } else {
    return p[i].sibling;
  }
}

int main() {
  int n, k, c, top, a, cur, no;
  int l, r;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    p[i].parent = -1;
    p[i].left = -1;
    p[i].right = -1;
    p[i].sibling = -1;
    p[i].type = "internal node";
  }

  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &no, &l, &r);

    if (l != -1) {
      p[no].left = l;
      p[l].parent = no;
    }
    if (r != -1) {
      p[no].right = r;
      p[r].parent = no;
    }
    if (l != -1 && r != -1) {
      p[l].sibling = r;
      p[r].sibling = l;
    }
  }

  for (int i = 0; i < n; i++) {
    height_cnt = -1;
    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d",
           i, parent_search(i), sibling_id(i), degree_search(i),
           depth_search(i), height_search(i));
    printf(", %s\\n", p[i].type.c_str());
  }

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

int height_cnt;

struct node {
  int id;
  // int depth;
  // int height;
  // int degree;
  string type;
  node *parent;
  node *left;
  node *right;
  node *sibling;
};

node *p[25];

int depth_search(node *p) {
  int cnt = 0;

  while (p->parent != NULL) {
    cnt++;
    p = p->parent;
  }

  return cnt;
}

int height_search(node *p) {
  if (p == NULL) {
    return 0;
  }

  height_cnt++;

  if (p->right == NULL && p->left == NULL) {
    return 0;
  } else {
    max(height_search(p->right), height_search(p->left));
  }

  return (int)log2((double)height_cnt);
}

int parent_search(node *p) {
  if (p->parent == NULL) {
    p->type = "root";
    return -1;
  } else {
    return p->parent->id;
  }
}

int degree_search(node *p) {
  if (p->left != NULL && p->right != NULL) {
    return 2;
  } else if (p->left != NULL || p->right != NULL) {
    return 1;
  } else {
    p->type = "leaf";
    return 0;
  }
}

int sibling_id(node *p) {
  if (p->sibling == NULL) {
    return -1;
  } else {
    return p->sibling->id;
  }
}

int main() {
  int n, k, c, top, a, cur;
  int l, r;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    p[i] = (node *)malloc(sizeof(node));
    p[i]->parent = NULL;
    p[i]->left = NULL;
    p[i]->right = NULL;
    p[i]->sibling = NULL;
    p[i]->type = "internal node";
  }

  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &p[i]->id, &l, &r);

    if (l != -1) {
      p[i]->left = p[l];
      p[l]->parent = p[i];
    }
    if (r != -1) {
      p[i]->right = p[r];
      p[r]->parent = p[i];
    }
    if (l != -1 && r != -1) {
      p[l]->sibling = p[r];
      p[r]->sibling = p[l];
    }
  }

  for (int i = 0; i < n; i++) {
    height_cnt = -1;
    printf("node %d:parent = %d, sibling = %d, degree = %d, depth = %d, height "
           "= %d",
           p[i]->id, parent_search(p[i]), sibling_id(p[i]), degree_search(p[i]),
           depth_search(p[i]), height_search(p[i]));
    printf(", %s\\n", p[i]->type.c_str());
  }

  return 0;
}