Problem p02280 (C++)

Buggy submission (status: Time Limit Exceeded):
#define FILE_INPUT_DEFINE
#ifdef FILE_INPUT_DEFINE
#include <fstream>
#endif // FILE_INPUT_DEFINE

#include <cstdio>
#include <iostream>

#define NIL (-1)
#define MAX 25

enum class RLI { eRoot, eInternalNode, eLeaf };

struct BinaryTree {
  int parent;
  int left;
  int right;
  int depth;
  int sibling;
  int degree;
  int height;
  RLI rli;

  BinaryTree() {
    parent = left = right = sibling = height = NIL;
    degree = 0;
  }
};

BinaryTree tree[MAX];

using namespace std;

int main() {
#ifdef FILE_INPUT_DEFINE
  // input from txt
  std::ifstream in(R"(C:\Temp\input.txt)");
  std::cin.rdbuf(in.rdbuf());
#endif // FILE_INPUT_DEFINE

  int n;
  cin >> n;

  int id, left, right;

  for (int i = 0; i < n; i++) {
    cin >> id >> left >> right;
    tree[id].left = left;
    tree[id].right = right;

    if (NIL != left)
      tree[left].parent = id;
    if (NIL != right)
      tree[right].parent = id;
  }

  for (int i = 0; i < n; ++i) {
    if (NIL == tree[i].parent) {
      tree[i].rli = RLI::eRoot;
      tree[i].depth = 0;
    } else {
      if ((NIL == tree[i].right) && (NIL == tree[i].left)) {
        tree[i].rli = RLI::eLeaf;
      } else {
        tree[i].rli = RLI::eInternalNode;
      }
    }

    if (RLI::eRoot != tree[i].rli) {
      int index = i;
      int depth = 0;
      while (NIL != tree[index].parent) {
        ++depth;
        index = tree[index].parent;
      }
      tree[i].depth = depth;

      if (tree[tree[i].parent].right == i) {
        tree[i].sibling = tree[tree[i].parent].left;
      } else {
        tree[i].sibling = tree[tree[i].parent].right;
      }
    }

    if (NIL != tree[i].left) {
      ++tree[i].degree;
    }

    if (NIL != tree[i].right) {
      ++tree[i].degree;
    }

    if ((tree[i].rli == RLI::eLeaf) || (1 == n)) {
      tree[i].height = 0;
    }
  }

  bool isLoop = true;

  while (isLoop) {
    isLoop = false;

    for (int i = 0; i < n; ++i) {
      if (NIL == tree[i].height) {
        int r = tree[tree[i].right].height;
        int l = tree[tree[i].left].height;

        if ((NIL != r) && (NIL != l)) {
          if (r >= l) {
            tree[i].height = r + 1;
          } else {
            tree[i].height = l + 1;
          }
        } else {
          isLoop = true;
        }
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d, ",
           i, tree[i].parent, tree[i].sibling, tree[i].degree, tree[i].depth,
           tree[i].height);
    switch (tree[i].rli) {
    case RLI::eRoot:
      printf("root\n");
      break;
    case RLI::eInternalNode:
      printf("internal node\n");
      break;
    case RLI::eLeaf:
      printf("leaf\n");
      break;
    default:
      break;
    }
  }

  return 0;
}

Fixed submission:
// #define		FILE_INPUT_DEFINE
#ifdef FILE_INPUT_DEFINE
#include <fstream>
#endif // FILE_INPUT_DEFINE

#include <cstdio>
#include <iostream>

#define NIL (-1)
#define MAX 25

enum class RLI { eRoot, eInternalNode, eLeaf };

struct BinaryTree {
  int parent;
  int left;
  int right;
  int depth;
  int sibling;
  int degree;
  int height;
  RLI rli;

  BinaryTree() {
    parent = left = right = sibling = height = NIL;
    degree = 0;
  }
};

BinaryTree tree[MAX];

using namespace std;

int main() {
#ifdef FILE_INPUT_DEFINE
  // input from txt
  std::ifstream in(R"(C:\Temp\input.txt)");
  std::cin.rdbuf(in.rdbuf());
#endif // FILE_INPUT_DEFINE

  int n;
  cin >> n;

  int id, left, right;

  for (int i = 0; i < n; i++) {
    cin >> id >> left >> right;
    tree[id].left = left;
    tree[id].right = right;

    if (NIL != left)
      tree[left].parent = id;
    if (NIL != right)
      tree[right].parent = id;
  }

  for (int i = 0; i < n; ++i) {
    if (NIL == tree[i].parent) {
      tree[i].rli = RLI::eRoot;
      tree[i].depth = 0;
    } else {
      if ((NIL == tree[i].right) && (NIL == tree[i].left)) {
        tree[i].rli = RLI::eLeaf;
      } else {
        tree[i].rli = RLI::eInternalNode;
      }
    }

    if (RLI::eRoot != tree[i].rli) {
      int index = i;
      int depth = 0;
      while (NIL != tree[index].parent) {
        ++depth;
        index = tree[index].parent;
      }
      tree[i].depth = depth;

      if (tree[tree[i].parent].right == i) {
        tree[i].sibling = tree[tree[i].parent].left;
      } else {
        tree[i].sibling = tree[tree[i].parent].right;
      }
    }

    if (NIL != tree[i].left) {
      ++tree[i].degree;
    }

    if (NIL != tree[i].right) {
      ++tree[i].degree;
    }

    if ((tree[i].rli == RLI::eLeaf) || (1 == n)) {
      tree[i].height = 0;
    }
  }

  bool isLoop = true;

  while (isLoop) {
    isLoop = false;

    for (int i = 0; i < n; ++i) {
      if (NIL == tree[i].height) {
        int r = tree[tree[i].right].height;
        int l = tree[tree[i].left].height;

        if ((NIL != r) && (NIL != l)) {
          if (r >= l) {
            tree[i].height = r + 1;
          } else {
            tree[i].height = l + 1;
          }
        } else {
          isLoop = true;
        }
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d, ",
           i, tree[i].parent, tree[i].sibling, tree[i].degree, tree[i].depth,
           tree[i].height);
    switch (tree[i].rli) {
    case RLI::eRoot:
      printf("root\n");
      break;
    case RLI::eInternalNode:
      printf("internal node\n");
      break;
    case RLI::eLeaf:
      printf("leaf\n");
      break;
    default:
      break;
    }
  }

  return 0;
}