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

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

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

int *D, *H;

Node *T;

void setdepth(int u, int p) {
  D[u] = p;
  if (T[u].right != -1) {
    setdepth(T[u].right, p + 1);
  }
  if (T[u].left != -1) {
    setdepth(T[u].left, p + 1);
  }
}

int setheight(int *H, int u) {
  int h1 = 0;
  int h2 = 0;

  if (T[u].right != -1)
    h1 = setheight(H, T[u].right) + 1;
  if (T[u].left != -1)
    h2 = setheight(H, T[u].left) + 1;

  return H[u] = max(h1, h2);
}

int main() {

  int n, x, r, y;

  cin >> n;

  T = new Node[n];

  D = new int[n];

  H = new int[n];

  for (int i = 0; i < n; i++) {
    T[i].parent = -1;
    T[i].left = -1;
    T[i].right = -1;
  }

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

    cin >> x;

    for (int i = 0; i < 2; i++) {
      cin >> y;
      T[y].parent = x;
      if (i == 0) {
        T[x].left = y;
      } else {
        T[x].right = y;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (T[i].parent == -1) {
      r = i;
      break;
    }
  }

  setdepth(r, 0);

  setheight(H, r);

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

    int ind;

    cout << "node " << i << ": parent = " << T[i].parent << ", ";

    int cnt = -1;

    for (int j = 0; j < n; j++) {
      if (T[j].parent == T[i].parent && i != j)
        cnt = j;
    }

    cout << "sibling = " << cnt << ", degree = ";

    if (T[i].left == -1 && T[i].right == -1)
      cout << 0;
    else if (T[i].left != -1 && T[i].right != -1)
      cout << 2;
    else
      cout << 1;

    cout << ", depth = " << D[i] << ", height = " << H[i];

    if (T[i].left == -1 && T[i].right == -1 && T[i].parent == -1) {
      cout << ", root";
    } else if (T[i].left == -1 && T[i].right == -1 && T[i].parent != -1) {
      cout << ", leaf";
    } else if (T[i].parent == -1) {
      cout << ", root";
    } else {
      cout << ", internal node";
    }

    cout << endl;
  }

  delete[] T, D, H;
  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

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

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

int *D, *H;

Node *T;

void setdepth(int u, int p) {
  D[u] = p;
  if (T[u].right != -1) {
    setdepth(T[u].right, p + 1);
  }
  if (T[u].left != -1) {
    setdepth(T[u].left, p + 1);
  }
}

int setheight(int *H, int u) {
  int h1 = 0;
  int h2 = 0;

  if (T[u].right != -1)
    h1 = setheight(H, T[u].right) + 1;
  if (T[u].left != -1)
    h2 = setheight(H, T[u].left) + 1;

  return H[u] = max(h1, h2);
}

int main() {

  int n, x, i, r, y;

  cin >> n;

  T = new Node[n];

  D = new int[n];

  H = new int[n];

  for (int i = 0; i < n; i++) {
    T[i].parent = -1;
    T[i].left = -1;
    T[i].right = -1;
  }

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

    cin >> x;

    for (int i = 0; i < 2; i++) {
      cin >> y;
      T[y].parent = x;
      if (i == 0) {
        T[x].left = y;
      } else {
        T[y].right = y;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (T[i].parent == -1) {
      r = i;
      break;
    }
  }

  setdepth(r, 0);

  setheight(H, r);

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

    int ind;

    cout << "node " << i << ": parent = " << T[i].parent << "degree = ";

    if (T[i].left == -1 && T[i].right == -1)
      cout << 0;
    else if (T[i].left != -1 && T[i].right != -1)
      cout << 2;
    else
      cout << 1;

    cout << ", depth = " << D[i] << ", height = " << H[i];

    if (T[i].left == -1 && T[i].right == -1 && T[i].parent == -1) {
      cout << ", root";
    } else if (T[i].left == -1 && T[i].right == -1 && T[i].parent != -1) {
      cout << ", leaf";
    } else if (T[i].parent == -1) {
      cout << ", root";
    } else {
      cout << ", internal node";
    }

    cout << endl;
  }

  delete[] T;
  return 0;
}