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

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

static const int MAX = 100000;

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

Node node[MAX];

int getDepth(int i) {
  int j = i;
  int d = 0;
  while (node[j].parent != -1) {
    j = node[j].parent;
    d++;
  }
  return d;
}

int getHeight(int i) {
  if (i == -1)
    return -1;
  else
    return 1 + max(getHeight(node[i].left), getHeight(node[i].right));
}

int main() {
  int n, a, tmp, deg, sib;
  cin >> n;
  for (int i = 0; i < n; i++) {
    node[i].parent = -1;
    node[i].left = -1;
    node[i].right = -1;
  }
  for (int i = 0; i < n; i++) {
    cin >> a;
    cin >> node[a].left;
    if (node[a].left != -1)
      node[node[a].left].parent = a;
    cin >> node[a].right;
    if (node[a].right != -1)
      node[node[a].right].parent = a;
  }

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << node[i].parent;
    tmp = node[i].parent;
    if (tmp == -1)
      sib = -1;
    else if (node[tmp].left == i)
      sib = node[tmp].right;
    else
      sib = node[tmp].left;
    cout << ", sibling = " << sib;
    deg = 0;
    if (node[i].left != -1)
      deg += 1;
    if (node[i].right != -1)
      deg += 1;
    cout << ", degree = " << deg;
    cout << ", depth = " << getDepth(i);
    cout << ", height = " << getHeight(i);
    if (node[i].parent == -1)
      cout << ", root";
    else if (node[i].left == -1 && node[i].right == -1)
      cout << ", leaf";
    else
      cout << ", internal node";
    cout << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

static const int MAX = 100000;

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

Node node[MAX];

int getDepth(int i) {
  int j = i;
  int d = 0;
  while (node[j].parent != -1) {
    j = node[j].parent;
    d++;
  }
  return d;
}

int getHeight(int i) {
  if (node[i].left == -1 && node[i].right == -1)
    return 0;
  return 1 + max(getHeight(node[i].left), getHeight(node[i].right));
}

int main() {
  int n, a, tmp, deg, sib;
  cin >> n;
  for (int i = 0; i < n; i++) {
    node[i].parent = -1;
    node[i].left = -1;
    node[i].right = -1;
  }
  for (int i = 0; i < n; i++) {
    cin >> a;
    cin >> node[a].left;
    if (node[a].left != -1)
      node[node[a].left].parent = a;
    cin >> node[a].right;
    if (node[a].right != -1)
      node[node[a].right].parent = a;
  }

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << node[i].parent;
    tmp = node[i].parent;
    if (tmp == -1)
      sib = -1;
    else if (node[tmp].left == i)
      sib = node[tmp].right;
    else
      sib = node[tmp].left;
    cout << ", sibling = " << sib;
    deg = 0;
    if (node[i].left != -1)
      deg += 1;
    if (node[i].right != -1)
      deg += 1;
    cout << ", degree = " << deg;
    cout << ", depth = " << getDepth(i);
    cout << ", height = " << getHeight(i);
    if (node[i].parent == -1)
      cout << ", root";
    else if (node[i].left == -1 && node[i].right == -1)
      cout << ", leaf";
    else
      cout << ", internal node";
    cout << endl;
  }

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

#include <iostream>
using namespace std;
#include <stdlib.h>

int *L, *R, *H; // left(child), right(child), height.

int height(int i) {
  if (i < 0)
    return -1;
  if (H[i] >= 0) {
    return H[i];
  } // already determined.
  else {
    H[i] = max(height(L[i]), height(R[i])) + 1;
    return H[i];
  }
}

int main() {
  int *P; // parent.
  int i, n, l, r, d, p;
  cin >> n;
  P = new int[n];
  L = new int[n];
  R = new int[n];
  H = new int[n];
  if (P == NULL || L == NULL || R == NULL || H == NULL)
    exit(EXIT_FAILURE);

  // initialize.
  for (i = 0; i < n; i++) {
    P[i] = H[i] = -1;
  }
  // input.
  while (cin >> i >> l >> r) {
    L[i] = l;
    R[i] = r;
    if (l >= 0)
      P[l] = i;
    if (r >= 0)
      P[r] = i;
  };

  for (i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << P[i] << ", sibling = ";

    if (P[i] < 0) {
      cout << -1;
    } else if (L[P[i]] < 0 || R[P[i]] < 0) {
      cout << -1;
    } else {
      cout << L[P[i]] + R[P[i]] - i;
    }

    cout << ", degree = ";
    if (L[i] < 0 && R[i] < 0) {
      cout << 0;
    } else if (L[i] < 0 || R[i] < 0) {
      cout << 1;
    } else {
      cout << 2;
    }

    cout << ", depth = ";
    d = 0;
    p = i;
    while (P[p] >= 0) {
      p = P[p];
      d++;
    };

    cout << d << ", height = " << height(i) << ", ";
    if (P[i] < 0) {
      cout << "root";
    } else if (L[i] < 0 && R[i] < 0) {
      cout << "leaf";
    } else {
      cout << "internal node";
    }
    cout << endl;
  }
  delete[] P;
  delete[] L;
  delete[] R;
  delete[] H;
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <iostream>
using namespace std;
#include <stdlib.h>

int *L, *R, *H; // left(child), right(child), height.

int height(int i) {
  if (i < 0)
    return -1;
  if (H[i] >= 0) {
    return H[i];
  } // already determined.
  else {
    H[i] = max(height(L[i]), height(R[i])) + 1;
    return H[i];
  }
}

int main() {
  int *P; // parent.
  int i, n, l, r, d, p;
  cin >> n;
  P = new int[n];
  L = new int[n];
  R = new int[n];
  H = new int[n];
  if (P == NULL || L == NULL || R == NULL || H == NULL)
    exit(EXIT_FAILURE);

  // initialize.
  for (i = 0; i < n; i++) {
    P[i] = L[i] = R[i] = H[i] = -1;
  }
  // input.
  while (cin >> i >> l >> r) {
    L[i] = l;
    R[i] = r;
    P[l] = i;
    P[r] = i;
  };

  for (i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << P[i] << ", sibling = ";
    if (P[i] < 0) {
      cout << -1;
    } else {
      cout << P[L[i]] + P[R[i]] - i;
    }
    cout << ", degree = ";
    if (L[i] < 0) {
      cout << 0;
    } else {
      cout << 2;
    }
    cout << ", depth = ";
    d = 0;
    p = i;
    while (P[p] >= 0) {
      p = P[p];
      d++;
    };
    cout << d << ", height = " << height(i) << ", ";
    if (P[i] < 0) {
      cout << "root";
    } else if (L[i] < 0) {
      cout << "leaf";
    } else {
      cout << "internal node";
    }
    cout << endl;
  }
  delete[] P;
  delete[] L;
  delete[] R;
  delete[] H;
  return 0;
}
Problem p02280 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define N 100001
#define INF 10000
using namespace std;

struct Node {
  int p, l, r;
};
Node t[N];

int sibling(int x) { return t[x].r; }

int dep(int x) {
  int cnt = 0;
  if (t[x].p >= 0)
    cnt = dep(t[x].p) + 1;
  return cnt;
}

int height(int x) {
  int cntl = 0, cntr = 0;
  if (t[x].l >= 0)
    cntl = height(t[x].l) + 1;
  if (t[x].l >= 0 && t[t[x].l].r >= 0)
    cntr = height(t[t[x].l].r) + 1;
  if (cntl < cntr)
    return cntr;
  else
    return cntl;
}

int degree(int x) {
  int cnt = 0;
  if (t[x].l >= 0)
    cnt++;
  if (t[x].l >= 0 && t[t[x].l].r >= 0)
    cnt++;
  return cnt;
}

int main() {
  int n, id, c;
  cin >> n;
  for (int i = 0; i < n; i++)
    t[i].l = -1, t[i].p = -1, t[i].r = -1;

  for (int i = 0; i < n; i++) {
    cin >> id;
    for (int j = 0; j < 2; j++) {
      int befc = c;
      cin >> c;
      if (c >= 0)
        t[c].p = id;
      if (j > 0 && c >= 0 && befc >= 0)
        t[befc].r = c, t[c].r = befc;
      else if (j == 0)
        t[id].l = c;
      if (t[id].l < 0 && j > 0)
        t[id].l = c;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << t[i].p
         << ", sibling = " << sibling(i) << ", degree = " << degree(i)
         << ", depth = ";
    cout << dep(i) << ", ";
    cout << "height = " << height(i) << ", ";
    if (t[i].l >= 0 && t[i].p >= 0)
      cout << "internal node";
    else if (t[i].p == -1)
      cout << "root";
    else
      cout << "leaf";
    cout << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#define N 100000
using namespace std;

struct Node {
  int p, l, r;
};
Node t[N];

int sibling(int x) { return t[x].r; }

int dep(int x) {
  int cnt = 0;
  if (t[x].p >= 0)
    cnt = dep(t[x].p) + 1;
  return cnt;
}

int height(int x) {
  int cntl = 0, cntr = 0;
  if (t[x].l >= 0)
    cntl = height(t[x].l) + 1;
  if (t[t[x].l].r >= 0)
    cntr = height(t[t[x].l].r) + 1;
  if (cntl < cntr)
    return cntr;
  else
    return cntl;
}

int degree(int x) {
  int cnt = 0;
  if (t[x].l >= 0)
    cnt++;
  if (t[t[x].l].r >= 0)
    cnt++;
  return cnt;
}

int main() {
  int n, id, c;
  cin >> n;
  for (int i = 0; i < n; i++)
    t[i].l = -1, t[i].p = -1, t[i].r = -1;

  for (int i = 0; i < n; i++) {
    cin >> id;
    for (int j = 0; j < 2; j++) {
      int befc = c;
      cin >> c;
      t[c].p = id;
      if (j > 0)
        t[befc].r = c, t[c].r = befc;
      else
        t[id].l = c;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << t[i].p
         << ", sibling = " << sibling(i) << ", degree = " << degree(i)
         << ", depth = ";
    cout << dep(i) << ", ";
    cout << "height = " << height(i) << ", ";
    if (t[i].l >= 0 && t[i].p != -1)
      cout << "internal node";
    else if (t[i].p == -1)
      cout << "root";
    else
      cout << "leaf";
    cout << endl;
  }

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

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

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

Node T[MAX];
int D[MAX], H[MAX];

void print(int num) {
  int deg, s;
  if (T[num].l != -1 && T[num].r != -1)
    deg = 2;
  else if (T[num].l == -1 && T[num].r == -1)
    deg = 0;
  else
    deg = 1;

  if (T[num].p == -1)
    s = -1;
  else if (T[T[num].p].l != num)
    s = T[T[num].p].l;
  else
    s = T[T[num].p].r;
  printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height "
         "= %d, ",
         num, T[num].p, s, deg, D[num], H[num]);

  if (T[num].p == -1)
    printf("root\\n");
  else if (T[num].l != -1 || T[num].r != -1)
    printf("internal node\\n");
  else
    printf("leaf\\n");
}

int depth_height(int num, int depth) {
  int m_d1 = -1, m_d2 = -1;
  D[num] = depth;
  if (T[num].l != -1)
    m_d1 = depth_height(T[num].l, depth + 1);
  if (T[num].r != -1)
    m_d2 = depth_height(T[num].r, depth + 1);

  if (m_d1 == -1 && m_d2 == -1) {
    H[num] = 0;
    return depth;
  } else if (m_d1 >= m_d2) {
    H[num] = m_d1 - depth;
    return m_d1;
  } else {
    H[num] = m_d2 - depth;
    return m_d2;
  }
}

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

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

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

  while (T[num].p != -1)
    num = T[num].p;
  j = depth_height(num, 0);

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


Error: TLE

Fixed submission:

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

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

Node T[MAX];
int D[MAX], H[MAX];

void print(int num) {
  int deg, s;
  if (T[num].l != -1 && T[num].r != -1)
    deg = 2;
  else if (T[num].l == -1 && T[num].r == -1)
    deg = 0;
  else
    deg = 1;

  if (T[num].p == -1)
    s = -1;
  else if (T[T[num].p].l != num)
    s = T[T[num].p].l;
  else
    s = T[T[num].p].r;
  printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height "
         "= %d, ",
         num, T[num].p, s, deg, D[num], H[num]);

  if (T[num].p == -1)
    printf("root\\n");
  else if (T[num].l != -1 || T[num].r != -1)
    printf("internal node\\n");
  else
    printf("leaf\\n");
}

int depth_height(int num, int depth) {
  int m_d1 = -1, m_d2 = -1;
  D[num] = depth;
  if (T[num].l != -1)
    m_d1 = depth_height(T[num].l, depth + 1);
  if (T[num].r != -1)
    m_d2 = depth_height(T[num].r, depth + 1);

  if (m_d1 == -1 && m_d2 == -1) {
    H[num] = 0;
    return depth;
  } else if (m_d1 >= m_d2) {
    H[num] = m_d1 - depth;
    return m_d1;
  } else {
    H[num] = m_d2 - depth;
    return m_d2;
  }
}

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

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

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

  for (int i = 0; i < n; i++) {
    print(i);
  }
  return 0;
}
Problem p02281 (C++) — buggy submission, verdict: Runtime Error

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

void Preorder(vector<vector<int>> &T, int root) {
  cout << " " << root;
  if (T[root][0] != -1)
    Preorder(T, T[root][0]);
  if (T[root][1] != -1)
    Preorder(T, T[root][1]);
  return;
}

void Inorder(vector<vector<int>> &T, int root) {
  if (T[root][0] != -1)
    Inorder(T, T[root][0]);
  cout << " " << root;
  if (T[root][1] != -1)
    Inorder(T, T[root][1]);
  return;
}

void Postorder(vector<vector<int>> &T, int root) {
  if (T[root][0] != -1)
    Postorder(T, T[root][0]);
  if (T[root][1] != -1)
    Postorder(T, T[root][1]);
  cout << " " << root;
  return;
}

int main() {
  int n, i, j, k, d, e;
  cin >> n;

  vector<vector<int>> T(n);
  vector<int> check(n, 0);

  for (i = 0; i < n; i++) {
    cin >> k;
    for (j = 0; j < 2; j++) {
      cin >> e;
      T[k].push_back(e);
      if (e != -1)
        check[e] = 1;
    }
  }

  int r;
  for (i = 0; i < n; i++) {
    if (check[i] == 0) {
      r = i;
      break;
    }
  }
  cout << "Preorder" << endl;
  Preorder(T, r);
  cout << endl;
  cout << "Inorder" << endl;
  Inorder(T, r);
  cout << endl;
  cout << "Postorder" << endl;
  Postorder(T, r);
  cout << endl;

  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

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

void Preorder(vector<vector<int>> &T, int root) {
  cout << " " << root;
  if (T[root][0] != -1)
    Preorder(T, T[root][0]);
  if (T[root][1] != -1)
    Preorder(T, T[root][1]);
  return;
}

void Inorder(vector<vector<int>> &T, int root) {
  if (T[root][0] != -1)
    Inorder(T, T[root][0]);
  cout << " " << root;
  if (T[root][1] != -1)
    Inorder(T, T[root][1]);
  return;
}

void Postorder(vector<vector<int>> &T, int root) {
  if (T[root][0] != -1)
    Postorder(T, T[root][0]);
  if (T[root][1] != -1)
    Postorder(T, T[root][1]);
  cout << " " << root;
  return;
}

int main() {
  int n, i, j, k, d, e;
  cin >> n;

  vector<vector<int>> T(n);
  vector<int> check(n, 0);

  for (i = 0; i < n; i++) {
    cin >> k;
    for (j = 0; j < 2; j++) {
      cin >> e;
      T[k].push_back(e);
      check[e] = 1;
    }
  }

  int r;
  for (i = 0; i < n; i++) {
    if (check[i] == 0) {
      r = i;
      break;
    }
  }
  cout << "Preorder" << endl;
  Preorder(T, r);
  cout << endl;
  cout << "Inorder" << endl;
  Inorder(T, r);
  cout << endl;
  cout << "Postorder" << endl;
  Postorder(T, r);
  cout << endl;

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#define ll long long
#define MAX 10000
#define NIL -1
using namespace std;

struct Node {
  int p, r, l;
};
Node T[MAX];
int n;

void preOrder(int u) {
  if (u == NIL)
    return;
  printf(" %d", u);
  preOrder(T[u].l);
  preOrder(T[u].r);
}

void postOrder(int u) {
  if (u == NIL)
    return;
  postOrder(T[u].l);
  postOrder(T[u].r);
  printf(" %d", u);
}

void inOrder(int u) {
  if (u == NIL)
    return;
  inOrder(T[u].l);
  printf(" %d", u);
  inOrder(T[u].r);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    T[i].p = NIL;
  }
  for (int i = 0; i < n; i++) {
    int v, l, r;
    cin >> v >> l >> r;
    T[v].l = l;
    T[v].r = r;
    if (l != NIL)
      T[l].p = v;
    if (r != NIL)
      T[r].p = v;
  }
  int root;
  for (int i = 0; i < n; i++)
    if (T[i].p == NIL)
      root = i;
  printf("Preorder\\n");
  preOrder(root);
  printf("\\n");
  printf("Inorder\\n");
  inOrder(root);
  printf("\\n");
  printf("Postorder\\n");
  postOrder(root);
  printf("\\n");
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#define ll long long
#define MAX 10000
#define NIL -1
using namespace std;

struct Node {
  int p, r, l;
};
Node T[MAX];
int n;

void preOrder(int u) {
  if (u == NIL)
    return;
  printf(" %d", u);
  preOrder(T[u].l);
  preOrder(T[u].r);
}

void postOrder(int u) {
  if (u == NIL)
    return;
  postOrder(T[u].l);
  postOrder(T[u].r);
  printf(" %d", u);
}

void inOrder(int u) {
  if (u == NIL)
    return;
  inOrder(T[u].l);
  printf(" %d", u);
  inOrder(T[u].r);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int v, l, r;
    cin >> v >> l >> r;
    T[v].l = l;
    T[r].r = r;
  }
  int root;
  for (int i = 0; i < n; i++)
    if (T[i].p == NIL)
      root = i;
  printf("Preorder\\n");
  preOrder(root);
  printf("Inorder\\n");
  inOrder(root);
  printf("Postorder\\n");
  postOrder(root);
}
Problem p02281 (C++) — buggy submission, verdict: Runtime Error

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

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

int order(vector<binarytree> &node, int i, int mode) {
  int e1, e2;

  if (mode == 0)
    cout << " " << i;

  e1 = node[i].left;
  if (e1 != -1)
    order(node, e1, mode);
  if (mode == 1)
    cout << " " << i;

  e2 = node[i].right;
  if (e2 != -1)
    order(node, e2, mode);
  if (mode == 2)
    cout << " " << i;
  return 0;
}

int main(void) {
  int n, e, tmp;
  stringstream ss;

  cin >> n;
  vector<binarytree> node(n);
  for (int i = 0; i < n; i++) {
    node[i].parent = -1;
    node[i].left = -1;
    node[i].right = -1;
  }

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

    cin >> tmp;
    node[e].left = tmp;
    if (tmp != -1)
      node[tmp].parent = e;

    cin >> tmp;
    node[e].right = tmp;
    if (tmp != -1)
      node[tmp].parent = e;
  }

  e = 0;
  tmp = node[e].parent;
  while (tmp != -1) {
    e = tmp;
    tmp = node[e].parent;
  }

  cout << "Preorder" << endl;
  order(node, e, 0);
  cout << endl;

  cout << "Inorder" << endl;
  order(node, e, 1);
  cout << endl;

  cout << "Postorder" << endl;
  order(node, e, 2);
  cout << endl;

  return 0;
}

Error: 0

Fixed submission:

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

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

int order(vector<binarytree> &node, int i, int mode) {
  int e1, e2;

  if (mode == 0)
    cout << " " << i;

  e1 = node[i].left;
  if (e1 != -1)
    order(node, e1, mode);
  if (mode == 1)
    cout << " " << i;

  e2 = node[i].right;
  if (e1 != -1)
    order(node, e2, mode);
  if (mode == 2)
    cout << " " << i;
  return 0;
}

int main(void) {
  int n, e, tmp;
  stringstream ss;

  cin >> n;
  vector<binarytree> node(n);
  for (int i = 0; i < n; i++) {
    node[i].parent = -1;
    node[i].left = -1;
    node[i].right = -1;
  }

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

    cin >> tmp;
    node[e].left = tmp;
    if (tmp != -1)
      node[tmp].parent = e;

    cin >> tmp;
    node[e].right = tmp;
    if (tmp != -1)
      node[tmp].parent = e;
  }

  e = 0;
  tmp = node[e].parent;
  while (tmp != -1) {
    e = tmp;
    tmp = node[e].parent;
  }

  cout << "Preorder" << endl;
  order(node, e, 0);
  cout << endl;

  cout << "Inorder" << endl;
  order(node, e, 1);
  cout << endl;

  cout << "Postorder" << endl;
  order(node, e, 2);
  cout << endl;

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

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

int x[1000][5]; // 0=left,1=right,2=parent,3=sibling;

void S1(int u) {
  if (u == -1) {
    return;
  }
  cout << ' ' << u;
  S1(x[u][0]);
  S1(x[u][1]);
}

void S2(int u) {
  if (u == -1) {
    return;
  }
  S2(x[u][0]);
  cout << ' ' << u;
  S2(x[u][1]);
}

void S3(int u) {
  if (u == -1) {
    return;
  }
  S3(x[u][0]);
  S3(x[u][1]);
  cout << ' ' << u;
}

int main() {
  memset(x, -1, sizeof(x));
  int n, a, c;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cin >> x[a][0] >> x[a][1];
    x[x[a][0]][2] = a;
    x[x[a][1]][2] = a;
    x[x[a][0]][3] = x[a][1];
    x[x[a][1]][3] = x[a][0];
  }
  cout << "Preorder" << endl;
  for (int i = 0; i < n; i++) {
    if (x[i][2] == -1) {
      c = i;
      break;
    }
  }
  S1(c);
  cout << endl;
  cout << "Inorder" << endl;
  S2(c);
  cout << endl;
  cout << "Postorder" << endl;
  S3(c);
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int x[1000][5]; // 0=left,1=right,2=parent,3=sibling;

void S1(int u) {
  if (u == -1) {
    return;
  }
  cout << ' ' << u;
  S1(x[u][0]);
  S1(x[u][1]);
}

void S2(int u) {
  if (u == -1) {
    return;
  }
  S2(x[u][0]);
  cout << ' ' << u;
  S2(x[u][1]);
}

void S3(int u) {
  if (u == -1) {
    return;
  }
  S3(x[u][0]);
  S3(x[u][1]);
  cout << ' ' << u;
}

int main() {
  memset(x, -1, sizeof(x));
  int n, a, c;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cin >> x[i][0] >> x[i][1];
    x[x[i][0]][2] = a;
    x[x[i][0]][3] = x[i][1];
    x[x[i][1]][3] = x[i][0];
  }
  cout << "Preorder" << endl;
  for (int i = 0; i < n; i++) {
    if (x[i][2] == -1) {
      c = i;
      break;
    }
  }
  S1(c);
  cout << endl;
  cout << "Inorder" << endl;
  S2(c);
  cout << endl;
  cout << "Postorder" << endl;
  S3(c);
  cout << endl;
  return 0;
}
Problem p02281 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

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

const int N = 25, NIL = -1;
Node node[N];

void preOrder(int id) {
  if (id == NIL) {
    return;
  }
  printf(" %d", id);
  preOrder(node[id].left);
  preOrder(node[id].right);
}

void inOrder(int id) {
  if (id == NIL) {
    return;
  }
  inOrder(node[id].left);
  printf(" %d", id);
  inOrder(node[id].right);
}

void postOrder(int id) {
  if (id == NIL) {
    return;
  }
  postOrder(node[id].left);
  postOrder(node[id].right);
  printf(" %d", id);
}

int main() {
  for (int i = 0; i < N; i++) {
    node[i].parent = NIL;
  }
  int n, id, left, right, root;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &id, &left, &right);
    node[id].left = left;
    node[id].right = right;
    if (left != NIL) {
      node[left].parent = id;
    }
    if (right != NIL) {
      node[right].parent = id;
    }
  }
  root = 0;
  while (node[root].parent != NIL) {
    root++;
  }
  printf("Preorder\\n");
  preOrder(root);
  printf("\\n");
  printf("Inorder\\n");
  inOrder(root);
  printf("\\n");
  printf("Postorder\\n");
  postOrder(root);
  printf("\\n");

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>

const int N = 25, NIL = -1;
typedef struct Node {
  int left;
  int right;
} Node;
Node node[N];

void preOrder(int id) {
  printf(" %d", id);
  if (node[id].left == NIL && node[id].right == NIL) {
    return;
  }
  preOrder(node[id].left);
  preOrder(node[id].right);
}

void inOrder(int id) {
  if (node[id].left == NIL && node[id].right == NIL) {
    printf(" %d", id);
    return;
  }
  inOrder(node[id].left);
  printf(" %d", id);
  inOrder(node[id].right);
}

void postOrder(int id) {
  if (node[id].left == NIL && node[id].right == NIL) {
    printf(" %d", id);
    return;
  }
  postOrder(node[id].left);
  postOrder(node[id].right);
  printf(" %d", id);
}

int main() {
  int n, id, root;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &id);
    if (i == 0) {
      root = id;
    }
    scanf("%d %d", &node[id].left, &node[id].right);
  }
  printf("Preorder\\n");
  preOrder(root);
  printf("\\n");
  printf("Inorder\\n");
  inOrder(root);
  printf("\\n");
  printf("Postorder\\n");
  postOrder(root);
  printf("\\n");

  return 0;
}