alexjercan/bugnet huggingface dataset:
Problem p02281 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define MAX 25
#define NIL -1

struct Node {
  int p, l, r;
};

Node T[MAX];

void preorder(int n) {
  printf(" %d", n);
  if (T[n].l != -1) {
    preorder(T[n].l);
  }
  if (T[n].r != -1) {
    preorder(T[n].r);
  }
}

void inorder(int n) {
  if (T[n].l != -1) {
    inorder(T[n].l);
  }
  printf(" %d", n);
  if (T[n].r != -1) {
    inorder(T[n].r);
  }
}

void postorder(int n) {
  if (T[n].l != -1) {
    postorder(T[n].l);
  }
  if (T[n].r != -1) {
    postorder(T[n].r);
  }
  printf(" %d", n);
}

int main() {
  int num, i, j, n;
  scanf("%d", &n);

  for (i = 0; i < n; i++)
    T[i].p = T[i].l = T[i].r = -1;

  for (i = 0; i < n; i++) {
    scanf("%d", &j);
    scanf("%d", &num);
    T[j].l = num;
    T[T[j].l].p = j;
    scanf("%d", &num);
    T[j].r = num;
    T[T[j].r].p = j;
  }

  while (T[num].p != -1)
    num = T[num].p;

  printf("Preorder\\n");
  preorder(num);
  printf("\\n");
  printf("");
  printf("Inorder\\n");
  inorder(num);
  printf("\\n");
  printf("");
  printf("Postorder\\n");
  postorder(num);
  printf("\\n");
  printf("");

  return 0;
}


Error: -11

Fixed submission:

#include <stdio.h>
#define MAX 25
#define NIL -1

struct Node {
  int p, l, r;
};

Node T[MAX];

void preorder(int n) {
  printf(" %d", &n);
  if (T[n].l != -1) {
    preorder(T[n].l);
  }
  if (T[n].r != -1) {
    preorder(T[n].r);
  }
}

void inorder(int n) {
  if (T[n].l != -1) {
    inorder(T[n].l);
  }
  printf(" %d", &n);
  if (T[n].r != -1) {
    inorder(T[n].r);
  }
}

void postorder(int n) {
  if (T[n].l != -1) {
    postorder(T[n].l);
  }
  printf(" %d", &n);
  if (T[T[n].p].r != -1) {
    postorder(T[n].l);
  }
}

int main() {
  int num, i, j, n;
  scanf("%d", &n);

  for (i = 0; i < n; i++)
    T[i].p = T[i].l = T[i].r = -1;

  for (i = 0; i < n; i++) {
    scanf("%d", &j);
    scanf("%d", &num);
    T[j].l = num;
    T[T[j].l].p = j;
    scanf("%d", &num);
    T[j].r = num;
    T[T[j].r].p = j;
  }

  while (T[num].p != -1)
    num = T[num].p;

  printf("Preorder\\n");
  preorder(num);
  printf("\\n");
  printf("");
  printf("Inorder\\n");
  inorder(num);
  printf("\\n");
  printf("");
  printf("Postorder\\n");
  postorder(num);
  printf("\\n");
  printf("");

  return 0;
}
Problem p02281 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
using namespace std;

typedef struct Tree {
  int p = -1, l = -1, r = -1;
} Tree;

Tree tree[25];
int n;

int root() {
  for (int i = 0; i < n; i++) {
    if (tree[i].p == -1)
      return i;
  }
  return -1;
}

void prtw(int r) {
  if (r == -1)
    return;
  printf(" %d", r);
  prtw(tree[r].l);
  prtw(tree[r].r);
}
void intw(int r) {
  if (r == -1)
    return;
  intw(tree[r].l);
  printf(" %d", r);
  intw(tree[r].r);
}
void potw(int r) {
  if (r == -1)
    return;
  potw(tree[r].l);
  potw(tree[r].r);
  printf(" %d", r);
}

int main() {
  scanf("%d", &n);
  int id, l, r;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &id, &l, &r);
    tree[id].l = l;
    tree[id].r = r;
    if (l != -1)
      tree[l].p = id;
    if (r != -1)
      tree[r].p = id;
  }
  int rt = root();
  printf("Preorder\\n");
  prtw(rt);
  printf("\\nInorder\\n");
  intw(rt);
  printf("\\nPostorder\\n");
  potw(rt);
  printf("\\n");

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

typedef struct T {
  int op, l, r;
} T;

T t[25];
int n, d;

void prt(int p) {
  if (d == 0) {
    cout << t[p].op;
  } else {
    cout << " " << t[p].op;
  }
}

void pre(int p = 0) {
  prt(p);
  d++;
  if (t[p].l != -1)
    pre(t[p].l);
  if (t[p].r != -1)
    pre(t[p].r);
}

void ino(int p = 0) {
  if (t[p].l != -1)
    ino(t[p].l);
  prt(p);
  d++;
  if (t[p].r != -1)
    ino(t[p].r);
}

void pos(int p = 0) {
  if (t[p].l != -1)
    pos(t[p].l);
  if (t[p].r != -1)
    pos(t[p].r);
  prt(p);
  d++;
}

int main() {
  cin >> n;
  int id, l, r, pt = 0;
  for (int i = 0; i < n; i++) {
    cin >> id >> l >> r;
    t[pt++] = {id, l, r};
  }
  d = 0;
  cout << "Preorder" << endl;
  pre();
  d = 0;
  cout << endl << "Inorder" << endl;
  ino();
  d = 0;
  cout << endl << "Postorder" << endl;
  pos();
  cout << endl;

  return 0;
}
Problem p02281 (C++) — buggy submission, verdict: Runtime Error


#define MAXN (25 + 10)
#include <cstdio>

struct Tree {
  int l;
  int r;
  int fa;
  Tree() : l(-1), r(-1), fa(-1) {}
} tree[MAXN];

void add(Tree &aim, int lef, int rig) {
  aim.l = lef;
  aim.r = rig;
}
int N;
int origin;

void Preorder(int oper) {
  printf(" %d", oper);
  if (tree[oper].l != -1)
    Preorder(tree[oper].l);
  if (tree[oper].r != -1)
    Preorder(tree[oper].r);
}
void Inorder(int oper) {
  if (tree[oper].l != -1)
    Inorder(tree[oper].l);
  printf(" %d", oper);
  if (tree[oper].r != -1)
    Inorder(tree[oper].r);
}
void Postorder(int oper) {
  if (tree[oper].l != -1)
    Postorder(tree[oper].l);
  if (tree[oper].r != -1)
    Postorder(tree[oper].r);
  printf(" %d", oper);
}
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    int ord, lef, rig;
    scanf("%d%d%d", &ord, &lef, &rig);
    add(tree[ord], lef, rig);
    tree[lef].fa = ord;
    tree[rig].fa = ord;
  }
  for (int i = 0; i < N; ++i)
    if (tree[i].fa == -1) {
      origin = i;
      break;
    }
  printf("Preorder\\n");
  Preorder(origin);
  printf("\\nInorder\\n");
  Inorder(origin);
  printf("\\nPostorder\\n");
  Postorder(origin);
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 80000 + 2
struct Node {
  int le;
  int ri;
};
Node jl[MAXN];
int tot = 0;
int hxq[MAXN], hxz[MAXN], hxh[MAXN];
int n;
int g;
void zg() {
  for (int i = 0; i < n; ++i) {
    int q = 0;
    if (jl[i].le == -1 || jl[i].ri == -1)
      continue;
    for (int j = 0; j < n; ++j) {
      if (jl[j].le == i || jl[j].ri == i)
        q = 1;
    }
    if (q != 1) {
      g = i;
      return;
    }
  }
}
void input() {
  cin >> n;
  int x;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    cin >> jl[x].le >> jl[x].ri;
  }
}
void syf1(int x) {
  if (jl[x].le == -1 && jl[x].ri == -1) {
    hxq[tot] = x;
    tot++;
    return;
  }
  hxq[tot] = x;
  tot++;
  syf1(jl[x].le);
  syf1(jl[x].ri);
  return;
}
void syf2(int x) {
  if (jl[x].le == -1 && jl[x].ri == -1) {
    hxz[tot] = x;
    tot++;
    return;
  }
  syf2(jl[x].le);
  hxz[tot] = x;
  tot++;
  syf2(jl[x].ri);
}
void syf3(int x) {
  if (jl[x].le == -1 && jl[x].ri == -1) {
    hxh[tot] = x;
    tot++;
    return;
  }
  syf3(jl[x].le);
  syf3(jl[x].ri);
  hxh[tot] = x;
  tot++;
}
void output() {
  cout << "Preorder" << endl;
  for (int i = 0; i < n; ++i) {
    cout << " " << hxq[i];
  }
  cout << endl;
  cout << "Inorder" << endl;
  for (int i = 0; i < n; ++i) {
    cout << " " << hxz[i];
  }
  cout << endl;
  cout << "Postorder" << endl;
  for (int i = 0; i < n; ++i) {
    cout << " " << hxh[i];
  }
  cout << endl;
}
int main() {
  input();
  zg();
  syf1(g);
  tot = 0;
  syf2(g);
  tot = 0;
  syf3(g);
  output();
  return 0;
}
Problem p02281 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

#define NIL -1
#define MAX 30

using namespace std;

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

Node node[MAX];

void preorder(int u) {
  if (u == NIL)
    return;

  cout << " " << u;
  preorder(node[u].left);
  preorder(node[u].right);
}

void inorder(int u) {
  if (u == NIL)
    return;
  inorder(node[u].left);
  cout << " " << u;
  inorder(node[u].right);
}

void postorder(int u) {
  if (u == NIL)
    return;
  postorder(node[u].left);
  postorder(node[u].right);
  cout << " " << u;
}

void init() {
  for (int i = 0; i < MAX; i++) {
    node[i].left = node[i].right = node[i].parent = NIL;
  }
}

signed main() {
  int n;
  int id, l, r;
  cin >> n;
  init();
  for (int i = 0; i < (int)n; i++) {
    cin >> id >> l >> r;
    node[id].left = l;
    node[id].right = r;

    if (l != NIL)
      node[l].parent = id;
    if (r != NIL)
      node[r].parent = id;
  }

  int root = 0;
  for (int i = 0; i < (int)n; i++) {
    if (node[i].parent == NIL) {
      root = i;
      break;
    }
  }
  cout << "Preorder" << endl;
  preorder(root);
  cout << "\\n";
  cout << "Inorder" << endl;
  inorder(root);
  cout << "\\n";
  cout << "Postorder" << endl;
  postorder(root);
  cout << "\\n";
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

void preorder(int x);
void inorder(int x);
void postorder(int x);

struct M {
  int id;
  int left;
  int right;
};
M a[25];

int main() {
  int n, i, l, r, id;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> id >> l >> r;
    a[i].left = l;
    a[i].right = r;
    if (l != -1)
      a[l].id = id;
    if (r != -1)
      a[r].id = id;
  }
  int root;
  for (int i = 0; i < n; i++) {
    if (a[i].id == -1) {
      root = i;
      break;
    }
  }
  cout << "Preorder" << endl << " ";
  preorder(root);
  cout << endl << "Inorder" << endl << " ";
  inorder(root);
  cout << endl << "Postorder" << endl << " ";
  postorder(root);
  cout << endl;
}

void preorder(int x) {
  if (x == -1) {
    return;
  }
  cout << x << " ";
  preorder(a[x].left);
  preorder(a[x].right);
}
void inorder(int x) {
  if (x == -1) {
    return;
  }

  inorder(a[x].left);
  cout << x << " ";
  inorder(a[x].right);
}

void postorder(int x) {
  if (x == -1) {
    return;
  }

  postorder(a[x].left);
  postorder(a[x].right);
  cout << x << " ";
}
Problem p02281 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

typedef struct {
  int num;
  int left;
  int right;
} nodeinfo;

vector<nodeinfo> nodes;

void output_preorder(int from, int n) {
  if (from == -1)
    return;
  int idx;
  for (int i = 0; i < n; i++)
    if (nodes[i].num == from)
      idx = i;
  int left = nodes[idx].left;
  int right = nodes[idx].right;
  cout << " " << from;
  output_preorder(left, n);
  output_preorder(right, n);
}

void output_inorder(int from, int n) {
  if (from == -1)
    return;
  int idx;
  for (int i = 0; i < n; i++)
    if (nodes[i].num == from)
      idx = i;
  int left = nodes[idx].left;
  int right = nodes[idx].right;
  output_inorder(left, n);
  cout << " " << from;
  output_inorder(right, n);
}

void output_postorder(int from, int n) {
  if (from == -1)
    return;
  int idx;
  for (int i = 0; i < n; i++)
    if (nodes[i].num == from)
      idx = i;
  int left = nodes[idx].left;
  int right = nodes[idx].right;
  output_postorder(left, n);
  output_postorder(right, n);
  cout << " " << from;
}

int main() {
  int n;
  cin >> n;
  vector<bool> root(n, true);
  for (int i = 0; i < n; i++) {
    nodeinfo node;
    int num, left, right;
    cin >> num >> left >> right;
    if (left != -1)
      root[left] = false;
    if (right != -1)
      root[right] = false;
    node.num = num;
    node.left = left;
    node.right = right;
    nodes.push_back(node);
  }
  int idx;
  for (int i = 0; i < n; i++)
    if (root[i])
      idx = i;
  cout << "Preorder" << endl;
  output_preorder(idx, n);
  cout << endl;
  cout << "Inorder" << endl;
  output_inorder(idx, n);
  cout << endl;
  cout << "Postorder" << endl;
  output_postorder(idx, n);
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

typedef struct {
  int num;
  int left;
  int right;
} nodeinfo;

vector<nodeinfo> nodes;

void output_preorder(int from) {
  if (from == -1)
    return;
  int left = nodes[from].left;
  int right = nodes[from].right;
  cout << " " << from;
  output_preorder(left);
  output_preorder(right);
}

void output_inorder(int from) {
  if (from == -1)
    return;
  int left = nodes[from].left;
  int right = nodes[from].right;
  output_inorder(left);
  cout << " " << from;
  output_inorder(right);
}

void output_postorder(int from) {
  if (from == -1)
    return;
  int left = nodes[from].left;
  int right = nodes[from].right;
  output_postorder(left);
  output_postorder(right);
  cout << " " << from;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    nodeinfo node;
    int num, left, right;
    cin >> num >> left >> right;
    node.num = num;
    node.left = left;
    node.right = right;
    nodes.push_back(node);
  }
  cout << "Preorder" << endl;
  output_preorder(0);
  cout << endl;
  cout << "Inorder" << endl;
  output_inorder(0);
  cout << endl;
  cout << "Postorder" << endl;
  output_postorder(0);
  cout << endl;
  return 0;
}
Problem p02281 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <string>
#include <vector>

#define ft first
#define sd second

using namespace std;

typedef pair<int, int> P;

map<int, P> mpp;

void preorder(int p) {
  cout << " " << p;
  if (mpp[p].ft != -1)
    preorder(mpp[p].ft);
  if (mpp[p].sd != -1)
    preorder(mpp[p].sd);
}

void inorder(int p) {
  if (mpp[p].ft != -1)
    inorder(mpp[p].ft);
  cout << " " << p;
  if (mpp[p].sd != -1)
    inorder(mpp[p].sd);
}

void postorder(int p) {
  if (mpp[p].ft != -1)
    postorder(mpp[p].ft);
  if (mpp[p].sd != -1)
    postorder(mpp[p].sd);
  cout << " " << p;
}

int rootcheck(vector<int> vec) {
  bool f;

  for (int i = 0; i < vec.size(); ++i) {
    f = false;
    for (map<int, P>::iterator it = mpp.begin(); it != mpp.end() && !f; ++it)
      f = (it->sd.ft == vec[i] || it->sd.sd == vec[i]);
    if (!f)
      return vec[i];
  }

  return 0;
}

int main() {
  int n, s, t, u, m;
  vector<int> vec;

  while (cin >> n && n) {
    m = -1;
    while (n--) {
      cin >> s >> t >> u;
      mpp[s] = P(t, u);
      vec.push_back(s);
    }
    m = rootcheck(vec);

    cout << "Preorder" << endl;
    preorder(m);
    cout << endl;
    cout << "Inorder" << endl;
    inorder(m);
    cout << endl;
    cout << "Postorder" << endl;
    postorder(m);
    cout << endl;

    mpp.clear();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>

#define ft first
#define sd second

using namespace std;

typedef pair<int, int> P;

map<int, P> mpp;

void preorder(int p) {
  cout << " " << p;
  if (mpp[p].ft != -1)
    preorder(mpp[p].ft);
  if (mpp[p].sd != -1)
    preorder(mpp[p].sd);
}

void inorder(int p) {
  if (mpp[p].ft != -1)
    inorder(mpp[p].ft);
  cout << " " << p;
  if (mpp[p].ft != -1)
    inorder(mpp[p].sd);
}

void postorder(int p) {
  if (mpp[p].ft != -1)
    postorder(mpp[p].ft);
  if (mpp[p].sd != -1)
    postorder(mpp[p].sd);
  cout << " " << p;
}

int main() {
  int n, s, t, u, m;

  while (cin >> n && n) {
    m = -1;
    while (n--) {
      cin >> s >> t >> u;
      mpp[s] = P(t, u);
      if (m == -1)
        m = s;
    }
    cout << "Preorder" << endl;
    preorder(m);
    cout << endl;
    cout << "Inorder" << endl;
    inorder(m);
    cout << endl;
    cout << "Postorder" << endl;
    postorder(m);
    cout << endl;

    mpp.clear();
  }

  return 0;
}
Problem p02281 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli n;
mat t;
vll p;
lli s;

void preorder(lli x) {
  if (x < 0)
    return;
  cout << " " << x;
  preorder(t[x][0]);
  preorder(t[x][1]);
}

void inorder(lli x) {
  if (x < 0)
    return;
  inorder(t[x][0]);
  cout << " " << x;
  inorder(t[x][1]);
}

void postorder(lli x) {
  if (x < 0)
    return;
  postorder(t[x][0]);
  postorder(t[x][1]);
  cout << " " << x;
}

int main() {
  cin >> n;
  p = vll(n, -1);
  t = mat(n, vll(2, -1));
  for (lli i = 0; i < n; i++) {
    lli id, left, right;
    cin >> id >> left >> right;
    t[id][0] = left;
    t[id][1] = right;
    if (left >= 0)
      p[left] = id;
    if (right >= 0)
      p[right] = id;
  }
  for (lli i = 0; i < n; i++) {
    if (p[i] < 0) {
      s = i;
      break;
    }
  }
  cout << "Preorder" << endl;
  preorder(s);
  cout << endl;
  cout << "Inorder" << endl;
  inorder(s);
  cout << endl;
  cout << "Postorder" << endl;
  postorder(s);
  cout << endl;

  return 0;
}

Error: -6
free(): invalid size


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli n;
mat t;
vll p;
lli s;

void preorder(lli x) {
  if (x < 0)
    return;
  cout << " " << x;
  preorder(t[x][0]);
  preorder(t[x][1]);
}

void inorder(lli x) {
  if (x < 0)
    return;
  inorder(t[x][0]);
  cout << " " << x;
  inorder(t[x][1]);
}

void postorder(lli x) {
  if (x < 0)
    return;
  postorder(t[x][0]);
  postorder(t[x][1]);
  cout << " " << x;
}

int main() {
  cin >> n;
  p = vll(n, -1);
  t = mat(n, vll(2, -1));
  for (lli i = 0; i < n; i++) {
    lli id, left, right;
    cin >> id >> left >> right;
    t[id][0] = left;
    t[id][1] = right;
    p[left] = id;
    p[right] = id;
  }
  for (lli i = 0; i < n; i++) {
    if (p[i] < 0) {
      s = i;
      break;
    }
  }
  /*
  cout << "Preorder" << endl;
  preorder(s);
  cout << endl;
  cout << "Inorder" << endl;
  inorder(s);
  cout << endl;
  cout << "Postorder" << endl;
  postorder(s);
  cout << endl;
  */

  return 0;
}
Problem p02281 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

constexpr int MAX = 25;
constexpr int NIL = -1;

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

Node node[MAX];

void preorder(int u) {
  if (u == NIL)
    return;
  cout << " " << u;
  preorder(node[u].left);
  preorder(node[u].right);
}

void inorder(int u) {
  if (u == NIL)
    return;
  inorder(node[u].left);
  cout << " " << u;
  inorder(node[u].right);
}

void postorder(int u) {
  if (u == NIL)
    return;
  postorder(node[u].left);
  postorder(node[u].right);
  cout << " " << u;
}

int main() {
  int n, id, l, r;
  cin >> n;
  for (int i = 0; i < n; i++)
    node[i].parent = NIL;
  for (int i = 0; i < n; i++) {
    cin >> id >> l >> r;
    node[id].left = l;
    node[id].right = r;
    if (l != NIL)
      node[l].parent = id;
    if (r != NIL)
      node[r].parent = id;
  }
  int root;
  for (int i = 0; i < n; i++)
    if (node[i].parent == NIL)
      root = i;

  cout << "Preorder" << endl;
  preorder(root);
  cout << endl;
  cout << "Inorder" << endl;
  inorder(root);
  cout << endl;
  cout << "Postorder" << endl;
  postorder(root);
  cout << endl;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

constexpr int MAX = 25;
constexpr int NIL = -1;

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

Node node[MAX];

void preorder(int u) {
  cout << u << " ";
  if (node[u].left != NIL)
    preorder(node[u].left);
  if (node[u].right != NIL)
    preorder(node[u].right);
}

void inorder(int u) {
  if (node[u].left != NIL)
    inorder(node[u].left);
  cout << u << " ";
  if (node[u].right != NIL)
    inorder(node[u].right);
}

void postorder(int u) {
  if (node[u].left != NIL)
    postorder(node[u].left);
  if (node[u].right != NIL)
    postorder(node[u].right);
  cout << u << " ";
}

int main() {
  int n, id, l, r;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> id >> l >> r;
    node[i].left = l;
    node[i].right = r;
    if (l != NIL)
      node[l].parent = id;
    if (r != NIL)
      node[r].parent = id;
  }
  int root;
  for (int i = 0; i < n; i++)
    if (node[i].parent == NIL)
      root = i;

  cout << "Preorder" << endl;
  preorder(root);
  cout << endl;
  cout << "Inorder" << endl;
  inorder(root);
  cout << endl;
  cout << "Postorder" << endl;
  postorder(root);
  cout << endl;
}
Problem p02281 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

class BinaryNode {
public:
  int left, right;
};

class BinaryTree {
public:
  int num;
  vector<BinaryNode> tree;
  vector<int> parent;

  BinaryTree(int _num) {
    num = _num;
    tree.resize(num);
    parent.resize(num);
    for (int i = 0; i < num; ++i)
      parent[i] = -1;
  }

  int top() {
    for (int i = 0; i < num; ++i) {
      if (parent[i] == -1) {
        return i;
      }
    }
  }

  void preorder(int n) {
    BinaryNode cur = tree[n];
    cout << " " << n;
    if (cur.left != -1) {
      preorder(cur.left);
    }
    if (cur.right != -1) {
      preorder(cur.right);
    }
  }
  void inorder(int n) {
    BinaryNode cur = tree[n];
    if (cur.left != -1) {
      inorder(cur.left);
    }
    cout << " " << n;
    if (cur.right != -1) {
      inorder(cur.right);
    }
  }
  void postorder(int n) {
    BinaryNode cur = tree[n];
    if (cur.left != -1) {
      postorder(cur.left);
    }
    if (cur.right != -1) {
      postorder(cur.right);
    }
    cout << " " << n;
  }
};

int main() {
  int n, id, left, right, top = -1;
  cin >> n;
  BinaryTree bTree = BinaryTree(n);
  for (int i = 0; i < n; ++i) {
    cin >> id >> left >> right;

    bTree.tree[id].left = left;
    bTree.tree[id].right = right;
    if (left + 1)
      bTree.parent[left] = id;
    if (right + 1)
      bTree.parent[right] = id;
  }

  top = bTree.top();

  cout << "Preorder" << endl;
  bTree.preorder(top);
  cout << endl;
  cout << "Inorder" << endl;
  bTree.inorder(top);
  cout << endl;
  cout << "Postorder" << endl;
  bTree.postorder(top);
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

class BinaryNode {
public:
  int left, right;
};

class BinaryTree {
public:
  int num;
  vector<BinaryNode> tree;

  BinaryTree(int _num) {
    num = _num;
    tree.resize(num);
  }

  void preorder(int n = 0) {
    BinaryNode cur = tree[n];
    cout << " " << n;
    if (cur.left != -1) {
      preorder(cur.left);
    }
    if (cur.right != -1) {
      preorder(cur.right);
    }
  }
  void inorder(int n = 0) {
    BinaryNode cur = tree[n];
    if (cur.left != -1) {
      preorder(cur.left);
    }
    cout << " " << n;
    if (cur.right != -1) {
      preorder(cur.right);
    }
  }
  void postorder(int n = 0) {
    BinaryNode cur = tree[n];
    if (cur.left != -1) {
      preorder(cur.left);
    }
    if (cur.right != -1) {
      preorder(cur.right);
    }
    cout << " " << n;
  }
};

int main() {
  int n, id, left, right;
  cin >> n;
  BinaryTree bTree = BinaryTree(n);
  for (int i = 0; i < n; ++i) {
    cin >> id >> left >> right;

    bTree.tree[i].left = left;
    bTree.tree[i].right = right;
  }

  cout << "preorder" << endl;
  bTree.preorder();
  cout << endl;
  cout << "inorder" << endl;
  bTree.inorder();
  cout << endl;
  cout << "postorder" << endl;
  bTree.postorder();
  cout << endl;
  return 0;
}