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

#include <cstdlib>
#include <iostream>
#include <list>

const int MAX_N = 25;

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

int main(void) {
  int n = 0;
  struct Node *N[MAX_N];

  std::cin >> n;

  for (int i = 0; i < n; i++) {
    struct Node *node = (struct Node *)std::malloc(sizeof(struct Node));

    node->parent = -1;
    node->left = -1;
    node->right = -1;

    N[i] = node;
  }

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

    std::cin >> id;

    struct Node *node = N[id];

    std::cin >> node->left;
    std::cin >> node->right;

    if (node->left >= 0) {
      N[node->left]->parent = id;
    }
    if (node->right >= 0) {
      N[node->right]->parent = id;
    }
  }

  for (int i = 0; i < n; i++) {
    struct Node *node = N[i];

    std::cout << "node " << i << ": ";
    std::cout << "parent = " << node->parent << ", ";

    std::cout << "sibling = ";

    if (node->parent >= 0) {
      struct Node *parent = N[node->parent];

      if (parent->left == i) {
        std::cout << parent->right;
      } else {
        std::cout << parent->left;
      }
    } else {
      std::cout << -1;
    }

    std::cout << ", ";
    std::cout << "degree = ";

    int degree = 0;

    if (node->left >= 0) {
      degree++;
    }
    if (node->right >= 0) {
      degree++;
    }

    std::cout << degree << ", ";

    int depth = 0;

    struct Node *p = node;

    while (p->parent >= 0) {
      p = N[p->parent];
      depth++;
    }

    std::cout << "depth = " << depth << ", ";

    int height = 0;
    std::list<int> nodes;

    nodes.push_back(i);

    std::cout << "height = ";

    while (!nodes.empty()) {
      std::list<int> newNodes;

      for (std::list<int>::iterator iterator = nodes.begin();
           iterator != nodes.end(); ++iterator) {
        if (*iterator < 0) {
          continue;
        }

        struct Node *v = N[*iterator];

        if (v->left >= 0) {
          newNodes.push_back(v->left);
        }
        if (v->right >= 0) {
          newNodes.push_back(v->right);
        }
      }

      if (newNodes.size() > 0) {
        height++;
      }

      nodes.clear();

      for (std::list<int>::iterator iterator = newNodes.begin();
           iterator != newNodes.end(); ++iterator) {
        if (*iterator >= 0) {
          nodes.push_back(*iterator);
        }
      }
    }

    std::cout << height << ", ";

    if (node->parent < 0) {
      std::cout << "root";
    } else if ((node->left >= 0) || (node->right >= 0)) {
      std::cout << "internal node";
    } else {
      std::cout << "leaf";
    }

    std::cout << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <list>

const int MAX_N = 25;

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

int main(void) {
  int n = 0;
  struct Node *N[MAX_N];

  std::cin >> n;

  for (int i = 0; i < n; i++) {
    struct Node *node = (struct Node *)std::malloc(sizeof(struct Node));

    node->parent = -1;
    node->left = -1;
    node->right = -1;

    N[i] = node;
  }

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

    struct Node *node = N[i];

    std::cin >> id;
    std::cin >> node->left;
    std::cin >> node->right;

    if (node->left > 0) {
      N[node->left]->parent = id;
    }
    if (node->right > 0) {
      N[node->right]->parent = id;
    }
  }

  for (int i = 0; i < n; i++) {
    struct Node *node = N[i];

    std::cout << "node " << i << ": ";
    std::cout << "parent = " << node->parent << ", ";

    std::cout << "sibling = ";

    if (node->parent >= 0) {
      struct Node *parent = N[node->parent];

      if (parent->left == i) {
        std::cout << parent->right;
      } else {
        std::cout << parent->left;
      }
    } else {
      std::cout << -1;
    }

    std::cout << ", ";
    std::cout << "degree = ";

    int degree = 0;

    if (node->left > 0) {
      degree++;
    }
    if (node->right > 0) {
      degree++;
    }

    std::cout << degree << ", ";

    int depth = 0;

    struct Node *p = node;

    while (p->parent >= 0) {
      p = N[p->parent];
      depth++;
    }

    std::cout << "depth = " << depth << ", ";

    int height = 0;
    std::list<int> nodes;

    if (node->left >= 0) {
      nodes.push_back(node->left);
    }
    if (node->right >= 0) {
      nodes.push_back(node->right);
    }

    std::cout << "height = ";

    while (!nodes.empty()) {
      height++;

      std::list<int> newNodes;

      for (std::list<int>::iterator iterator = nodes.begin();
           iterator != nodes.end(); ++iterator) {
        if (*iterator < 0) {
          continue;
        }

        struct Node *v = N[*iterator];

        if (v->left >= 0) {
          newNodes.push_back(v->left);
        }
        if (v->right >= 0) {
          newNodes.push_back(v->right);
        }
      }

      nodes.clear();

      for (std::list<int>::iterator iterator = newNodes.begin();
           iterator != newNodes.end(); ++iterator) {
        nodes.push_back(*iterator);
      }
    }

    std::cout << height << ", ";

    if (node->parent < 0) {
      std::cout << "root";
    } else if ((node->left > 0) || (node->right > 0)) {
      std::cout << "internal node";
    } else {
      std::cout << "leaf";
    }

    std::cout << std::endl;
  }

  return 0;
}