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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define NIL -1

struct Tree {
  int left, right, parent;
};

Tree *T;
int *S, *D;
int n;
int max = -1;

void depth(int n, int d) {
  if (n == NIL)
    return;
  D[n] = d;
  depth(T[n].left, d + 1);
  depth(T[n].right, d + 1);
}

int height(int n) {
  int h1 = 0, h2 = 0;
  if (T[n].left != NIL)
    h1 = height(T[n].left) + 1;
  if (T[n].right != NIL)
    h2 = height(T[n].right) + 1;
  return (h1 > h2 ? h1 : h2);
}

void print(int h, int r) {
  int deg;
  for (int i = 0; i < n; i++) {
    deg = 0;
    cout << "node " << i << ": ";
    cout << "parent = " << T[i].parent << ", ";
    if (i != r) {
      if (T[T[i].parent].left == i) {
        cout << "sibling = " << T[T[i].parent].right << ", ";
      } else {
        cout << "sibling = " << T[T[i].parent].left << ", ";
      }
    } else {
      cout << "sibling = -1, ";
    }
    if (T[i].left != NIL)
      deg++;
    if (T[i].right != NIL)
      deg++;
    cout << "degree = " << deg << ", ";
    cout << "depth = " << D[i] << ", ";
    cout << "height = " << height(i) << ", ";
    if (T[i].parent == NIL) {
      cout << "root" << endl;
    } else if (T[i].left != NIL || T[i].right != NIL) {
      cout << "internal node" << endl;
    } else {
      cout << "leaf" << endl;
    }
  }
}

int main() {
  cin >> n;
  T = new Tree[n];
  D = new int[n];
  int a, l, r;

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

  for (int i = 0; i < n; i++) {
    cin >> a >> l >> r;
    if (l != -1) {
      T[a].left = l;
      T[T[a].left].parent = a;
    }
    if (r != -1) {
      T[a].right = r;
      T[T[a].right].parent = a;
    }
  }

  int root;
  int max = -1;
  for (int i = 0; i < n; i++) {
    if (T[i].parent == NIL)
      root = i;
  }
  depth(root, 0);
  for (int i = 0; i < n; i++) {
    if (D[i] >= max)
      max = D[i];
  }
  print(max, root);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define NIL -1

struct Tree {
  int left, right, parent;
};

Tree *T;
int *S, *D;
int n;
int max = -1;
void depth(int n, int d) {
  D[n] = d;
  if (T[n].left != NIL)
    depth(T[n].left, d + 1);
  if (T[n].right != NIL)
    depth(T[n].right, d + 1);
}

void print(int h) {
  int deg;
  for (int i = 0; i < n; i++) {
    deg = 0;
    cout << "node " << i << ": ";
    cout << "parent = " << T[i].parent << ", ";
    if (T[T[i].parent].left == i) {
      cout << "sibling = " << T[T[i].parent].right << ", ";
    } else {
      cout << "sibling = " << T[T[i].parent].left << ", ";
    }
    if (T[i].left != NIL)
      deg++;
    if (T[i].right != NIL)
      deg++;
    cout << "degree = " << deg << ", ";
    cout << "depth = " << D[i] << ", ";
    cout << "height = " << h - D[i] << ", ";
    if (T[i].parent == NIL) {
      cout << "root" << endl;
    } else if (T[i].left != NIL || T[i].right != NIL) {
      cout << "internal node" << endl;
    } else {
      cout << "leaf" << endl;
    }
  }
}

int main() {
  cin >> n;
  T = new Tree[n];
  D = new int[n];
  int a, l, r;

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

  for (int i = 0; i < n; i++) {
    cin >> a >> l >> r;
    if (l != -1) {
      T[a].left = l;
      T[T[a].left].parent = a;
    }
    if (r != -1) {
      T[a].right = r;
      T[T[a].right].parent = a;
    }
  }

  int root;
  depth(root, 0);
  int max = -1;
  for (int i = 0; i < n; i++) {
    if (T[i].parent == NIL)
      root = i;
    if (D[i] >= max)
      max = D[i];
  }
  fprintf(stderr, "Check\\n");
  print(max);
  return 0;
}