Problem p02280 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#define MAX 10000
#define NIL -1

using namespace std;

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

Node T[MAX];
int Depth[MAX], Height[MAX];

void setDepth(int u, int d) {
  if (u == NIL)
    return;
  Depth[u] = d;
  setDepth(T[u].left, d + 1);
  setDepth(T[u].right, d + 1);
}

int setHeight(int u) {
  int h1 = 0, h2 = 0;
  if (T[u].left != NIL)
    h1 = setHeight(T[u].left) + 1;
  if (T[u].right != NIL)
    h2 = setHeight(T[u].right) + 1;
  Height[u] = max(h1, h2);
  return Height[u];
}

int getSibling(int u) {
  if (T[u].parent == NIL)
    return NIL;
  if (T[T[u].parent].left == u && T[T[u].parent].right != NIL)
    return T[T[u].parent].right;
  if (T[T[u].parent].right == u && T[T[u].parent].left != NIL)
    return T[T[u].parent].left;
  return NIL;
}

void print(int u) {
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].parent << ", ";
  cout << "sibling = " << getSibling(u) << ", ";
  int degree = 0;
  if (T[u].left != NIL)
    degree++;
  if (T[u].right != NIL)
    degree++;
  cout << "degree = " << degree << ", ";
  cout << "depth = " << Depth[u] << ", ";
  cout << "height = " << Height[u] << ", ";
  if (T[u].parent == NIL)
    cout << "root" << endl;
  else if (degree == 0)
    cout << "leaf" << endl;
  else
    cout << "internal node" << endl;
}

int main() {
  int n, i, p, x = 0;
  scanf("%d", &n);
  //    cin >> n;

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

  for (i = 0; i < n; i++) {
    scanf("%d", &p);
    scanf("%d%d", &T[p].left, &T[p].right);
    //        cin >> p >> T[i].left >> T[i].right;
    if (T[p].left != NIL)
      T[T[p].left].parent = p;
    if (T[p].right != NIL)
      T[T[p].right].parent = p;
  }

  for (i = 0; i < n; i++) {
    if (T[i].parent == NIL)
      x = i;
  }

  setDepth(x, 0);
  setHeight(x);

  for (i = 0; i < n; i++)
    print(i);

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#define MAX 10000
#define NIL -1

using namespace std;

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

Node T[MAX];
int Depth[MAX], Height[MAX];

void setDepth(int u, int d) {
  if (u == NIL)
    return;
  Depth[u] = d;
  setDepth(T[u].left, d + 1);
  setDepth(T[u].right, d + 1);
}

int setHeight(int u) {
  int h1 = 0, h2 = 0;
  if (T[u].left != NIL)
    h1 = setHeight(T[u].left) + 1;
  if (T[u].right != NIL)
    h2 = setHeight(T[u].right) + 1;
  Height[u] = max(h1, h2);
  return Height[u];
}

int getSibling(int u) {
  if (T[u].parent == NIL)
    return NIL;
  if (T[T[u].parent].left == u && T[T[u].parent].right != NIL)
    return T[T[u].parent].right;
  if (T[T[u].parent].right == u && T[T[u].parent].left != NIL)
    return T[T[u].parent].left;
  return NIL;
}

void print(int u) {
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].parent << ", ";
  cout << "sibling = " << getSibling(u) << ", ";
  int degree = 0;
  if (T[u].left != NIL)
    degree++;
  if (T[u].right != NIL)
    degree++;
  cout << "degree = " << degree << ", ";
  cout << "depth = " << Depth[u] << ", ";
  cout << "height = " << Height[u] << ", ";
  if (T[u].parent == NIL)
    cout << "root" << endl;
  else if (degree == 0)
    cout << "leaf" << endl;
  else
    cout << "internal node" << endl;
}

int main() {
  int n, i, p;
  cin >> n;

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

  for (i = 0; i < n; i++) {
    cin >> p >> T[i].left >> T[i].right;
    if (T[i].left != NIL)
      T[T[i].left].parent = p;
    if (T[i].right != NIL)
      T[T[i].right].parent = p;
  }

  setDepth(0, 0);
  setHeight(0);

  for (i = 0; i < n; i++)
    print(i);

  return 0;
}