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

#include <iostream>

using namespace std;
int n;

class Node {
public:
  int id;
  int p;
  int l;
  int r;
};

Node T[30];

void preOrder(int u) {
  if (u == -1) {
    return;
  }
  cout << " " << u;
  preOrder(T[u].l);
  preOrder(T[u].r);
}

void midOrder(int u) {
  if (u == -1) {
    return;
  }
  midOrder(T[u].l);
  cout << " " << u;
  midOrder(T[u].r);
}

void postOrder(int u) {
  if (u == -1) {
    return;
  }
  postOrder(T[u].l);
  postOrder(T[u].r);
  cout << " " << u;
}

int main() {
  cin >> n;
  int id, left, right;
  for (int i = 0; i < n; i++) {
    T[i].p = -1;
  }
  for (int i = 0; i < n; i++) {
    cin >> id >> left >> right;
    T[id].l = left;
    T[id].r = right;
    T[left].p = T[right].p = id;
  }
  int root;
  for (int i = 0; i < n; i++) {
    if (T[i].p == -1) {
      root = i;
    }
  }
  cout << "Preorder" << endl;
  preOrder(root);
  cout << endl << "Inorder" << endl;
  midOrder(root);
  cout << endl << "Postorder" << endl;
  postOrder(root);
  cout << endl;
  return 0;
}

Error: -6
free(): invalid pointer


Fixed submission:



#include <iostream>
#include <vector>

#define MAX 25

using namespace std;

void printPreorder(int current, vector<int> &lefts, vector<int> &rights,
                   vector<int> &ids) {

  cout << ids[current] << ' ';

  if (lefts[current] == -1) {

    return;
  }

  printPreorder(lefts[current], lefts, rights, ids);

  printPreorder(rights[current], lefts, rights, ids);
}

int getLeftEdge(int current, vector<int> &lefts) {

  if (lefts[current] == -1) {

    return current;
  }

  return getLeftEdge(lefts[current], lefts);
}

void printChilds(int currnt, vector<int> &lefts, vector<int> &rights) {

  cout << currnt << ' ';

  if (lefts[currnt] == -1) {

    return;
  }

  printChilds(lefts[currnt], lefts, rights);

  printChilds(rights[currnt], lefts, rights);
}

void printInorder(int current, vector<int> &parents, vector<int> &lefts,
                  vector<int> &rights, int sentinel, int rightEdge, int first) {

  cout << current << ' ';

  if (current == sentinel) {

    if (rights[current] != -1) {

      printChilds(rights[current], lefts, rights);
    }

    return;
  }

  if (current == first) {

    printInorder(rightEdge, parents, lefts, rights, sentinel, rightEdge, first);

  } else {

    if (rights[current] != -1) {

      printChilds(rights[current], lefts, rights);
    }

    printInorder(parents[current], parents, lefts, rights, sentinel, rightEdge,
                 first);
  }
}

void printPostOrder(int current, vector<int> &siblings, vector<int> &parent,
                    int sentinel, int rightEdge, int first) {

  cout << current << ' ';

  if (current == sentinel) {

    cout << first;

    return;
  }

  if (parent[current] == first) {

    printPostOrder(rightEdge, siblings, parent, sentinel, rightEdge, first);

  } else {

    cout << siblings[current] << ' ';

    printPostOrder(parent[current], siblings, parent, sentinel, rightEdge,
                   first);
  }
}

int main(void) {

  int n;
  int first = 0;
  vector<int> ids(MAX, 0);
  vector<int> lefts(MAX, 0);
  vector<int> rights(MAX, 0);
  vector<int> parents(MAX, 0);
  vector<int> siblings(MAX, 0);

  cin >> n;

  parents[0] = -1;
  siblings[0] = -1;

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

    int id;
    cin >> id;

    ids[id] = id;

    if (i == 0) {

      first = ids[id];
    }

    cin >> lefts[ids[id]];
    cin >> rights[ids[id]];

    parents[lefts[id]] = ids[id];
    parents[rights[id]] = ids[id];

    siblings[lefts[id]] = rights[id];
    siblings[rights[id]] = lefts[id];
  }

  cout << "Preorder" << endl;

  cout << ' ';

  printPreorder(first, lefts, rights, ids);

  cout << endl;

  int leftEdge = getLeftEdge(first, lefts);
  int rightEdge = getLeftEdge(rights[first], lefts);

  cout << "Inorder" << endl;

  cout << ' ';

  printInorder(leftEdge, parents, lefts, rights, rights[first], rightEdge,
               first);

  cout << endl;

  cout << "Postorder" << endl;

  cout << ' ';

  printPostOrder(leftEdge, siblings, parents, rights[first], rightEdge, first);

  cout << endl;

  return 0;
}