prithivMLmods/Coder-Stat huggingface dataset:
Problem p02279 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define MAX 10005
#define NIL -1

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

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

void print(int u) {
  int i, c;
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].p << ", ";
  cout << "depth = " << D[u] << ", ";

  if (T[u].p == NIL) {
    cout << "root, ";
  } else if (T[u].l == NIL) {
    cout << "leaf, ";
  } else {
    cout << "internal node, ";
  }

  cout << "[";

  for (i = 0, c = T[u].l; c != NIL; i++, c = T[c].r) {
    if (i) {
      cout << ", ";
    }
    cout << c;
  }
  cout << "]" << endl;
}

void rec(int u, int p) {
  D[u] = p;
  if (T[u].r != NIL) {
    rec(T[u].r, p);
  }
  if (T[u].l != NIL) {
    rec(T[u].l, p + 1);
  }
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    T[i].p = T[i].l = T[i].r = NIL;
  }

  int v, d, c, l, r;
  for (int i = 0; i < n; i++) {
    cin >> v >> d;
    for (int j = 0; j < d; j++) {
      cin >> c;
      if (j == 0) {
        T[v].l = c;
      } else {
        T[l].r = c;
      }
      l = c;
      T[c].p = v;
    }
  }
  for (int i = 0; i < n; i++) {
    if (T[i].p == NIL) {
      r = i;
    }
  }

  rec(r, 0);

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

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
#define MAX 100005
#define NIL -1

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

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

void print(int u) {
  int i, c;
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].p << ", ";
  cout << "depth = " << D[u] << ", ";

  if (T[u].p == NIL) {
    cout << "root, ";
  } else if (T[u].l == NIL) {
    cout << "leaf, ";
  } else {
    cout << "internal node, ";
  }

  cout << "[";

  for (i = 0, c = T[u].l; c != NIL; i++, c = T[c].r) {
    if (i) {
      cout << ", ";
    }
    cout << c;
  }
  cout << "]" << endl;
}

void rec(int u, int p) {
  D[u] = p;
  if (T[u].r != NIL) {
    rec(T[u].r, p);
  }
  if (T[u].l != NIL) {
    rec(T[u].l, p + 1);
  }
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    T[i].p = T[i].l = T[i].r = NIL;
  }

  int v, d, c, l, r;
  for (int i = 0; i < n; i++) {
    cin >> v >> d;
    for (int j = 0; j < d; j++) {
      cin >> c;
      if (j == 0) {
        T[v].l = c;
      } else {
        T[l].r = c;
      }
      l = c;
      T[c].p = v;
    }
  }
  for (int i = 0; i < n; i++) {
    if (T[i].p == NIL) {
      r = i;
    }
  }

  rec(r, 0);

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

  return 0;
}
Problem p02279 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
map<int, int> mpa, mpb;
#define MAX 100005
#define NIL -1

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

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

void print(int u) {
  int i, c;
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].p << ", ";
  cout << "depth = " << D[u] << ", ";
  if (T[u].p == NIL)
    cout << "root, ";
  else if (T[u].l == NIL)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";
  for (i = 0, c = T[u].l; c != NIL; i++, c = T[c].r) {
    if (i)
      cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}

int rec(int u, int p) {
  D[u] = p;
  if (T[u].r != NIL)
    rec(T[u].r, p); // 深さを入れてく
  if (T[u].l != NIL)
    rec(T[u].l, p + 1);
}

int main() {
  int i, j, d, v, c, l, r;
  cin >> n;
  for (i = 0; i < n; i++)
    T[i].p = T[i].l = T[i].r = NIL; // 初期化

  for (i = 0; i < n; i++) {
    cin >> v >> d;
    for (j = 0; j < v; j++) {
      cin >> c;
      if (j == 0)
        T[v].l = c;
      else
        T[l].r = c;
      l = c;
      T[c].p = v;
    }
  }
  for (i = 0; i < n; i++) {
    if (T[i].p == NIL)
      r = i;
  }
  rec(r, 0);
  for (i = 0; i < n; i++)
    print(i);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
map<int, int> mpa, mpb;
#define MAX 100005
#define NIL -1

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

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

void print(int u) {
  int i, c;
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].p << ", ";
  cout << "depth = " << D[u] << ", ";
  if (T[u].p == NIL)
    cout << "root, ";
  else if (T[u].l == NIL)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";
  for (i = 0, c = T[u].l; c != NIL; i++, c = T[c].r) {
    if (i)
      cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}

int rec(int u, int p) {
  D[u] = p;
  if (T[u].r != NIL)
    rec(T[u].r, p); // 深さを入れてく
  if (T[u].l != NIL)
    rec(T[u].l, p + 1);
}

int main() {
  int i, j, d, v, c, l, r;
  cin >> n;
  for (i = 0; i < n; i++)
    T[i].p = T[i].l = T[i].r = NIL; // 初期化

  for (i = 0; i < n; i++) {
    cin >> v >> d;
    for (j = 0; j < d; j++) {
      cin >> c;
      if (j == 0)
        T[v].l = c;
      else
        T[l].r = c;
      l = c;
      T[c].p = v;
    }
  }
  for (i = 0; i < n; i++) {
    if (T[i].p == NIL)
      r = i;
  }
  rec(r, 0);
  for (i = 0; i < n; i++)
    print(i);

  return 0;
}
Problem p02279 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define INT_MAX 2147483647
#define INT_MIN -2147483647

#define NUM 10000
#define INVALID_IDX -1
using namespace std;

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

TreeNode t[NUM];
int D[NUM];

void print(int idx) {
  cout << "node " << idx << ": ";
  cout << "parent = " << t[idx].p << ", ";
  cout << "depth = " << D[idx] << ", ";

  if (t[idx].p == INVALID_IDX)
    cout << "root, ";
  else if (t[idx].l == INVALID_IDX)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";
  for (int i = t[idx].l; i != INVALID_IDX; i = t[i].r) {
    cout << i;
    if (t[i].r != INVALID_IDX)
      cout << ", ";
  }
  cout << "]" << endl;
}

void getHeight(int idx, int depth) {
  D[idx] = depth;
  if (t[idx].r != INVALID_IDX)
    getHeight(t[idx].r, depth);
  if (t[idx].l != INVALID_IDX)
    getHeight(t[idx].l, depth + 1);
}

int main() {
  int i, j, n, idx0, idx1, d, l, r = INVALID_IDX;

  cin >> n;
  for (int i = 0; i < n; ++i)
    t[i].p = t[i].l = t[i].r = INVALID_IDX;

  for (int i = 0; i < n; ++i) {
    cin >> idx0 >> d;
    for (int j = 0; j < d; ++j) {
      cin >> idx1;
      if (0 == j)
        t[idx0].l = idx1;
      else
        t[l].r = idx1;
      l = idx1;
      t[idx1].p = idx0;
    }
  }

  for (int i = 0; i < n; ++i) {
    if (t[i].p == INVALID_IDX) {
      r = i;
      break;
    }
  }

  getHeight(r, 0);
  for (int i = 0; i < n; ++i)
    print(i);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define INT_MAX 2147483647
#define INT_MIN -2147483647

#define NUM 100005
#define INVALID_IDX -1
using namespace std;

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

TreeNode t[NUM];
int D[NUM];

void print(int idx) {
  cout << "node " << idx << ": ";
  cout << "parent = " << t[idx].p << ", ";
  cout << "depth = " << D[idx] << ", ";

  if (t[idx].p == INVALID_IDX)
    cout << "root, ";
  else if (t[idx].l == INVALID_IDX)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";
  for (int i = t[idx].l; i != INVALID_IDX; i = t[i].r) {
    cout << i;
    if (t[i].r != INVALID_IDX)
      cout << ", ";
  }
  cout << "]" << endl;
}

void getHeight(int idx, int depth) {
  D[idx] = depth;
  if (t[idx].r != INVALID_IDX)
    getHeight(t[idx].r, depth);
  if (t[idx].l != INVALID_IDX)
    getHeight(t[idx].l, depth + 1);
}

int main() {
  int i, j, n, idx0, idx1, d, l, r = INVALID_IDX;

  cin >> n;
  for (int i = 0; i < n; ++i)
    t[i].p = t[i].l = t[i].r = INVALID_IDX;

  for (int i = 0; i < n; ++i) {
    cin >> idx0 >> d;
    for (int j = 0; j < d; ++j) {
      cin >> idx1;
      if (0 == j)
        t[idx0].l = idx1;
      else
        t[l].r = idx1;
      l = idx1;
      t[idx1].p = idx0;
    }
  }

  for (int i = 0; i < n; ++i) {
    if (t[i].p == INVALID_IDX) {
      r = i;
      break;
    }
  }

  getHeight(r, 0);
  for (int i = 0; i < n; ++i)
    print(i);

  return 0;
}
Problem p02280 (C++)

Buggy submission (status: 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 (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;
}


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 (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;
}
Problem p02281 (C++)

Buggy submission (status: 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);
      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;
}

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);
      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;
}
Problem p02281 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02281 (C++)

Buggy submission (status: Memory Limit Exceeded):
// ALDS1_7_C.cpp
// Tree - Tree Walk

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

struct Node {
  int id;
  int lchild;
  int rchild;
  int parent = -1;
};

void Preorder(vector<Node> &nodes, int id) {
  cout << " " << nodes[id].id;
  if (nodes[id].lchild == -1 && nodes[id].rchild == -1) {
    return;
  }
  Preorder(nodes, nodes[id].lchild);
  Preorder(nodes, nodes[id].rchild);
}

void Inorder(vector<Node> &nodes, int id) {
  if (nodes[id].lchild != -1)
    Inorder(nodes, nodes[id].lchild);
  cout << " " << nodes[id].id;
  if (nodes[id].rchild != -1)
    Inorder(nodes, nodes[id].rchild);
}

void Postorder(vector<Node> &nodes, int id) {
  if (nodes[id].lchild != -1)
    Postorder(nodes, nodes[id].lchild);
  if (nodes[id].rchild != -1)
    Postorder(nodes, nodes[id].rchild);
  cout << " " << nodes[id].id;
}

int main() {
  int n;
  cin >> n;
  vector<Node> nodes(n);
  for (int i = 0; i < n; i++) {
    int id, lc, rc;
    cin >> id >> lc >> rc;
    nodes[id].id = id;
    nodes[id].lchild = lc;
    nodes[id].rchild = rc;
    if (lc != -1) {
      nodes[lc].parent = id;
    }
    if (rc != -1) {
      nodes[rc].parent = id;
    }
  }

  int root_id;
  for (int i = 0; i < n; i++) {
    if (nodes[i].parent == -1) {
      root_id = i;
      break;
    }
  }

  cout << "Preorder" << endl;
  Preorder(nodes, root_id);
  cout << endl << "Inorder" << endl;
  Inorder(nodes, root_id);
  cout << endl << "Postorder" << endl;
  Postorder(nodes, root_id);
  cout << endl;
  return 0;
}

Fixed submission:
// ALDS1_7_C.cpp
// Tree - Tree Walk

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

struct Node {
  int id;
  int lchild;
  int rchild;
  int parent = -1;
};

void Preorder(vector<Node> &nodes, int id) {
  cout << " " << nodes[id].id;
  if (nodes[id].lchild != -1)
    Preorder(nodes, nodes[id].lchild);
  if (nodes[id].rchild != -1)
    Preorder(nodes, nodes[id].rchild);
}

void Inorder(vector<Node> &nodes, int id) {
  if (nodes[id].lchild != -1)
    Inorder(nodes, nodes[id].lchild);
  cout << " " << nodes[id].id;
  if (nodes[id].rchild != -1)
    Inorder(nodes, nodes[id].rchild);
}

void Postorder(vector<Node> &nodes, int id) {
  if (nodes[id].lchild != -1)
    Postorder(nodes, nodes[id].lchild);
  if (nodes[id].rchild != -1)
    Postorder(nodes, nodes[id].rchild);
  cout << " " << nodes[id].id;
}

int main() {
  int n;
  cin >> n;
  vector<Node> nodes(n);
  for (int i = 0; i < n; i++) {
    int id, lc, rc;
    cin >> id >> lc >> rc;
    nodes[id].id = id;
    nodes[id].lchild = lc;
    nodes[id].rchild = rc;
    if (lc != -1) {
      nodes[lc].parent = id;
    }
    if (rc != -1) {
      nodes[rc].parent = id;
    }
  }

  int root_id;
  for (int i = 0; i < n; i++) {
    if (nodes[i].parent == -1) {
      root_id = i;
      break;
    }
  }

  cout << "Preorder" << endl;
  Preorder(nodes, root_id);
  cout << endl << "Inorder" << endl;
  Inorder(nodes, root_id);
  cout << endl << "Postorder" << endl;
  Postorder(nodes, root_id);
  cout << endl;
  return 0;
}
Problem p02281 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <vector>
enum order {
  preorder = 0,
  inorder = 1,
  postorder = 2,
};
class BinaryTree {
private:
  int numVertex;
  int *parent, *left, *right;

public:
  BinaryTree(int v);
  ~BinaryTree();
  void init();
  void dfs(int v, order flag);
  std::vector<int> root();
};
BinaryTree::BinaryTree(int v) {
  numVertex = v;
  parent = new int[v];
  left = new int[v];
  right = new int[v];
  for (int i = 0; i < v; i++) {
    parent[i] = -1;
    left[i] = -1;
    right[i] = -1;
  }
}
BinaryTree::~BinaryTree() {
  delete[] parent;
  delete[] left;
  delete[] right;
}
void BinaryTree::init() {
  for (int i = 0; i < numVertex; i++) {
    int id, l, r;
    scanf("%d%d%d", &id, &l, &r);
    parent[l] = id;
    parent[r] = id;
    left[id] = l;
    right[id] = r;
  }
}
std::vector<int> BinaryTree::root() {
  std::vector<int> vec;
  for (int i = 0; i < numVertex; i++) {
    if (parent[i] == -1)
      vec.push_back(i);
  }
  return vec;
}
void BinaryTree::dfs(int v, order flag) {
  if (v >= 0) {
    if (flag == preorder)
      printf(" %d", v);
    dfs(left[v], flag);
    if (flag == inorder)
      printf(" %d", v);
    dfs(right[v], flag);
    if (flag == postorder)
      printf(" %d", v);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  BinaryTree t(n);
  t.init();
  std::vector<int> v = t.root();
  printf("Preorder\n");
  t.dfs(v[0], preorder);
  printf("\nInorder\n");
  t.dfs(v[0], inorder);
  printf("\nPostorder\n");
  t.dfs(v[0], postorder);
  printf("\n");
  return 0;
}


Fixed submission:
#include <cstdio>
#include <vector>
enum order {
  preorder = 0,
  inorder = 1,
  postorder = 2,
};
class BinaryTree {
private:
  int numVertex;
  int *parent, *left, *right;

public:
  BinaryTree(int v);
  ~BinaryTree();
  void init();
  void dfs(int v, order flag);
  std::vector<int> root();
};
BinaryTree::BinaryTree(int v) {
  numVertex = v;
  parent = new int[v];
  left = new int[v];
  right = new int[v];
  for (int i = 0; i < v; i++) {
    parent[i] = -1;
    left[i] = -1;
    right[i] = -1;
  }
}
BinaryTree::~BinaryTree() {
  delete[] parent;
  delete[] left;
  delete[] right;
}
void BinaryTree::init() {
  for (int i = 0; i < numVertex; i++) {
    int id, l, r;
    scanf("%d%d%d", &id, &l, &r);
    if (l >= 0)
      parent[l] = id;
    if (r >= 0)
      parent[r] = id;
    left[id] = l;
    right[id] = r;
  }
}
std::vector<int> BinaryTree::root() {
  std::vector<int> vec;
  for (int i = 0; i < numVertex; i++) {
    if (parent[i] == -1)
      vec.push_back(i);
  }
  return vec;
}
void BinaryTree::dfs(int v, order flag) {
  if (v >= 0) {
    if (flag == preorder)
      printf(" %d", v);
    dfs(left[v], flag);
    if (flag == inorder)
      printf(" %d", v);
    dfs(right[v], flag);
    if (flag == postorder)
      printf(" %d", v);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  BinaryTree t(n);
  t.init();
  std::vector<int> v = t.root();
  printf("Preorder\n");
  t.dfs(v[0], preorder);
  printf("\nInorder\n");
  t.dfs(v[0], inorder);
  printf("\nPostorder\n");
  t.dfs(v[0], postorder);
  printf("\n");
  return 0;
}
Problem p02281 (C++)

Buggy submission (status: Runtime Error):
///
// File:  alds1_7_c.cpp
// Author: ymiyamoto
//
// Created on Sat Oct 14 18:52:33 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

struct tree_t {
  int32_t parent;
  int32_t left;
  int32_t right;
  tree_t() {
    parent = -1;
    left = -1;
    right = -1;
  }
};

#define SIZE 100
#define NIL -1

static tree_t tree[SIZE];
static vector<int32_t> preorder;
static vector<int32_t> inorder;
static vector<int32_t> postorder;

void walk(int32_t index) {
  preorder.push_back(index);
  if (tree[index].left != NIL) {
    walk(tree[index].left);
  }
  inorder.push_back(index);
  if (tree[index].right != NIL) {
    walk(tree[index].right);
  }
  postorder.push_back(index);
}

int32_t main() {
  int32_t n;
  cin >> n;

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

  int32_t root = NIL;
  for (int32_t i = 0; i < n; i++) {
    if (tree[i].parent == NIL) {
      root = i;
    }
  }

  walk(root);

  cout << "Preorder" << endl;
  for (uint32_t i = 0; i < preorder.size(); i++) {
    cout << " " << preorder[i];
  }
  cout << endl;

  cout << "Inorder" << endl;
  for (uint32_t i = 0; i < inorder.size(); i++) {
    cout << " " << inorder[i];
  }
  cout << endl;

  cout << "Postorder" << endl;
  for (uint32_t i = 0; i < postorder.size(); i++) {
    cout << " " << postorder[i];
  }
  cout << endl;

  return 0;
}

Fixed submission:
///
// File:  alds1_7_c.cpp
// Author: ymiyamoto
//
// Created on Sat Oct 14 18:52:33 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

struct tree_t {
  int32_t parent;
  int32_t left;
  int32_t right;
  tree_t() {
    parent = -1;
    left = -1;
    right = -1;
  }
};

#define SIZE 100
#define NIL -1

static tree_t tree[SIZE];
static vector<int32_t> preorder;
static vector<int32_t> inorder;
static vector<int32_t> postorder;

void walk(int32_t index) {
  preorder.push_back(index);
  if (tree[index].left != NIL) {
    walk(tree[index].left);
  }
  inorder.push_back(index);
  if (tree[index].right != NIL) {
    walk(tree[index].right);
  }
  postorder.push_back(index);
}

int32_t main() {
  int32_t n;
  cin >> n;

  for (int32_t i = 0; i < n; i++) {
    int32_t id, left, right;
    cin >> id >> left >> right;
    tree[id].left = left;
    tree[id].right = right;
    if (left != NIL) {
      tree[left].parent = id;
    }
    if (right != NIL) {
      tree[right].parent = id;
    }
  }

  int32_t root = NIL;
  for (int32_t i = 0; i < n; i++) {
    if (tree[i].parent == NIL) {
      root = i;
    }
  }

  walk(root);

  cout << "Preorder" << endl;
  for (uint32_t i = 0; i < preorder.size(); i++) {
    cout << " " << preorder[i];
  }
  cout << endl;

  cout << "Inorder" << endl;
  for (uint32_t i = 0; i < inorder.size(); i++) {
    cout << " " << inorder[i];
  }
  cout << endl;

  cout << "Postorder" << endl;
  for (uint32_t i = 0; i < postorder.size(); i++) {
    cout << " " << postorder[i];
  }
  cout << endl;

  return 0;
}
Problem p02281 (C++)

Buggy submission (status: 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 (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;
}

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 (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;
}
Problem p02282 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

struct node {
  int left, right;
} nodes[100];

int n, pre[105], in[105], inloc[105], cnt = 0;

int rec(int s, int e) {
  if (s < e) {
    int m = inloc[pre[cnt++]];
    int l = rec(s, m - 1);
    int r = rec(m + 1, e);
    nodes[in[m]].left = l;
    nodes[in[m]].right = r;
    return in[m];
  } else if (s == e) {
    return in[s];
  } else {
    return 0;
  }
}

void post(int r) {
  if (nodes[r].left != 0)
    post(nodes[r].left);
  if (nodes[r].right != 0)
    post(nodes[r].right);
  cout << r;
  if (r != pre[0])
    cout << " ";
}

int main() {
  memset(nodes, 0, sizeof(nodes));
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    cin >> pre[i];
  }
  for (i = 0; i < n; i++) {
    cin >> in[i];
    inloc[in[i]] = i;
  }
  int mm = rec(0, n - 1);
  post(pre[0]);
  cout << endl;
  return 0;
}
//
// int main(){
//    int a[10]={1,2,3,4,5,32,62,23,8,33};
//    int *b=find(a,a+10,62);
//    int c=distance(a,b);
//    cout<<c;
//}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

struct node {
  int left, right;
} nodes[100];

int n, pre[105], in[105], inloc[105], cnt = 0;

int rec(int s, int e) {
  if (s < e) {
    int m = inloc[pre[cnt++]];
    int l = rec(s, m - 1);
    int r = rec(m + 1, e);
    nodes[in[m]].left = l;
    nodes[in[m]].right = r;
    return in[m];
  } else if (s == e) {
    cnt++;
    return in[s];
  } else {
    return 0;
  }
}

void post(int r) {
  if (nodes[r].left != 0)
    post(nodes[r].left);
  if (nodes[r].right != 0)
    post(nodes[r].right);
  cout << r;
  if (r != pre[0])
    cout << " ";
}

int main() {
  memset(nodes, 0, sizeof(nodes));
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    cin >> pre[i];
  }
  for (i = 0; i < n; i++) {
    cin >> in[i];
    inloc[in[i]] = i;
  }
  int mm = rec(0, n - 1);
  post(pre[0]);
  cout << endl;
  return 0;
}
//
// int main(){
//    int a[10]={1,2,3,4,5,32,62,23,8,33};
//    int *b=find(a,a+10,62);
//    int c=distance(a,b);
//    cout<<c;
//}
Problem p02282 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

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

  Node() : id(), parent(-1), left(-1), right(-1) {}
};

static vector<Node> TREE(25);

static void inputNode(Node &node, int id) {
  node.id = id;
  cin >> node.left >> node.right;
}

static vector<int> preorder(25), inorder(25);

static void inputOrder(vector<int> &order, int num) {
  for (int i = 0; i < num; i++) {
    cin >> order[i];
  }
}

static int current = 0, outIndex = 0;

static int next() {
  int c = preorder[current];
  current++;
  return c;
}

static int find(int c) {
  auto it = find(inorder.begin(), inorder.end(), c);
  return distance(inorder.begin(), it);
}

static void reconstruction(int l, int r) {
  if (l >= r) {
    return;
  }
  int c = next();
  int m = find(c);
  reconstruction(l, m);
  reconstruction(m + 1, r);

  if (outIndex != 0) {
    cout << " " << c;
  } else {
    cout << c;
  }
  outIndex++;
}

int main() {
  int n;
  cin >> n;

  inputOrder(preorder, n);
  inputOrder(inorder, n);
  reconstruction(0, n);

  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

static vector<int> preorder(100), inorder(100);

static void inputOrder(vector<int> &order, int num) {
  for (int i = 0; i < num; i++) {
    cin >> order[i];
  }
}

static int current = 0, outIndex = 0;

static int next() {
  int c = preorder[current];
  current++;
  return c;
}

static int find(int c) {
  auto it = find(inorder.begin(), inorder.end(), c);
  return distance(inorder.begin(), it);
}

static void reconstruction(int l, int r) {
  if (l >= r) {
    return;
  }
  int c = next();
  int m = find(c);
  reconstruction(l, m);
  reconstruction(m + 1, r);

  if (outIndex != 0) {
    cout << " " << c;
  } else {
    cout << c;
  }
  outIndex++;
}

int main() {
  int n;
  cin >> n;

  inputOrder(preorder, n);
  inputOrder(inorder, n);
  reconstruction(0, n);

  cout << endl;

  return 0;
}
Problem p02282 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> pre[i];
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();

  return 0;
}
Problem p02282 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l = r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l == r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();

  return 0;
}
Problem p02282 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(1, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();
  return 0;
}
Problem p02282 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>

void to_post(int *A, int *B, int *C, int h) {
  int x = A[0];
  C[h - 1] = x;
  if (h < 2)
    return;
  int m = 0;
  while (B[m] != x)
    m++;
  if (m > 0)
    to_post(A + 1, B, C, m);
  if (h - m - 1 > 0)
    to_post(A + m + 1, B + m + 1, C + m, h - m - 1);
}

int main() {
  int *A, *B, *C;
  int i, n;
  scanf("%d", &n);

  A = new int[n];
  B = new int[n];
  C = new int[n];
  if (A == NULL || B == NULL || C == NULL)
    exit(EXIT_FAILURE);

  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    C[i] = 0;
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &B[n]);
  }

  to_post(A, B, C, n);

  printf("%d", C[0]);
  for (i = 1; i < n; i++) {
    printf(" %d", C[i]);
  }
  printf("\n");

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>

void to_post(int *A, int *B, int *C, int h) {
  int x = A[0];
  C[h - 1] = x;
  if (h < 2)
    return;
  int m = 0;
  while (B[m] != x)
    m++;
  if (m > 0)
    to_post(A + 1, B, C, m);
  if (h - m - 1 > 0)
    to_post(A + m + 1, B + m + 1, C + m, h - m - 1);
}

int main() {
  int *A, *B, *C;
  int i, n;
  scanf("%d", &n);

  A = new int[n];
  B = new int[n];
  C = new int[n];
  if (A == NULL || B == NULL || C == NULL)
    exit(EXIT_FAILURE);

  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    C[i] = 0;
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &B[i]);
  }

  to_post(A, B, C, n);

  printf("%d", C[0]);
  for (i = 1; i < n; i++) {
    printf(" %d", C[i]);
  }
  printf("\n");

  return 0;
}
Problem p02282 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + l, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }
  solve();
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main() {
  int k;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }
  solve();
}
Problem p02282 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

using namespace std;
typedef long long ll;

const int MAXN = 25;
const int NIL = -1;

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

Node T[MAXN];
int P[MAXN], I[MAXN], root;
int cnt = 0;

void postorder(int u) {
  if (T[u].l != NIL)
    postorder(T[u].l);
  if (T[u].r != NIL)
    postorder(T[u].r);
  if (cnt != 0)
    cout << " ";
  cout << u;
  cnt++;
}

void makeT(int u, int n, int head, int tail) {
  int p_place, i_place;

  rep(i, n) {
    if (P[i] == u)
      p_place = i;
    if (I[i] == u)
      i_place = i;
  }
  if (head != i_place) {
    T[u].l = P[p_place + 1];
    T[P[p_place + 1]].p = u;
    makeT(P[p_place + 1], n, head, i_place);
  }
  if (i_place + 1 != tail) {
    T[u].r = P[p_place + 1 + i_place - head];
    T[P[p_place + 1 + i_place - head]].p = u;
    makeT(P[p_place + 1 + i_place - head], n, i_place + 1, tail);
  }
}

int main() {
  int n;

  cin >> n;
  rep1(i, n) T[i].p = T[i].r = T[i].l = NIL;
  rep(i, n) cin >> P[i];
  rep(i, n) cin >> I[i];
  makeT(P[0], n, 0, n);

  rep1(i, n) {
    if (T[i].p == NIL)
      root = i;
  }
  postorder(root);
  cout << endl;
  return (0);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

using namespace std;
typedef long long ll;

const int MAXN = 50;
const int NIL = -1;

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

Node T[MAXN];
int P[MAXN], I[MAXN], root;
int cnt = 0;

void postorder(int u) {
  if (T[u].l != NIL)
    postorder(T[u].l);
  if (T[u].r != NIL)
    postorder(T[u].r);
  if (cnt != 0)
    cout << " ";
  cout << u;
  cnt++;
}

void makeT(int u, int n, int head, int tail) {
  int p_place, i_place;

  rep(i, n) {
    if (P[i] == u)
      p_place = i;
    if (I[i] == u)
      i_place = i;
  }
  if (head != i_place) {
    T[u].l = P[p_place + 1];
    T[P[p_place + 1]].p = u;
    makeT(P[p_place + 1], n, head, i_place);
  }
  if (i_place + 1 != tail) {
    T[u].r = P[p_place + 1 + i_place - head];
    T[P[p_place + 1 + i_place - head]].p = u;
    makeT(P[p_place + 1 + i_place - head], n, i_place + 1, tail);
  }
}

int main() {
  int n;

  cin >> n;
  rep1(i, n) T[i].p = T[i].r = T[i].l = NIL;
  rep(i, n) cin >> P[i];
  rep(i, n) cin >> I[i];
  makeT(P[0], n, 0, n);

  rep1(i, n) {
    if (T[i].p == NIL)
      root = i;
  }
  postorder(root);
  cout << endl;
  return (0);
}
Problem p02283 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

struct node {
  int lc;
  int rc;
  int key;
  void init(int a) {
    lc = rc = -1;
    key = a;
  }
};

#define bstN 200000
struct bst {

  int size;
  node t[bstN];

  void init() { size = 0; }

  void insert(int v) {
    int i = 0;
    while (i < size) {
      int &child = (v < t[i].key ? t[i].lc : t[i].rc);
      if (child == -1)
        child = size;
      i = child;
    }
    t[size++].init(v);
  }

  void preord(int pos, vector<int> &vec) {
    if (pos == -1 || pos == size)
      return;
    preord(t[pos].lc, vec);
    vec.push_back(t[pos].key);
    preord(t[pos].rc, vec);
  }

  void inord(int pos, vector<int> &vec) {
    if (pos == -1 || pos == size)
      return;
    vec.push_back(t[pos].key);
    inord(t[pos].lc, vec);
    inord(t[pos].rc, vec);
  }
};

bst T;

int main() {
  T.init();
  int n, a;
  char str[100];
  scanf("%d", &n);

  while (n--) {
    scanf("%s", str);
    if (str[0] == 'i') {
      scanf("%d", &a);
      T.insert(a);
    } else {
      vector<int> ans, ans2;

      T.preord(0, ans);
      for (int i : ans)
        cout << ' ' << i;
      cout << endl;

      T.inord(0, ans2);
      for (int i : ans2)
        cout << ' ' << i;
      cout << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

struct node {
  int lc;
  int rc;
  int key;
  void init(int a) {
    lc = rc = -1;
    key = a;
  }
};

#define bstN 600000
struct bst {

  int size;
  node t[bstN];

  void init() { size = 0; }

  void insert(int v) {
    int i = 0;
    while (i < size) {
      int &child = (v < t[i].key ? t[i].lc : t[i].rc);
      if (child == -1)
        child = size;
      i = child;
    }
    t[size++].init(v);
  }

  void preord(int pos, vector<int> &vec) {
    if (pos == -1 || pos == size)
      return;
    preord(t[pos].lc, vec);
    vec.push_back(t[pos].key);
    preord(t[pos].rc, vec);
  }

  void inord(int pos, vector<int> &vec) {
    if (pos == -1 || pos == size)
      return;
    vec.push_back(t[pos].key);
    inord(t[pos].lc, vec);
    inord(t[pos].rc, vec);
  }
};

bst T;

int main() {
  T.init();
  int n, a;
  char str[100];
  scanf("%d", &n);

  while (n--) {
    scanf("%s", str);
    if (str[0] == 'i') {
      scanf("%d", &a);
      T.insert(a);
    } else {
      vector<int> ans, ans2;

      T.preord(0, ans);
      for (int i : ans)
        cout << ' ' << i;
      cout << endl;

      T.inord(0, ans2);
      for (int i : ans2)
        cout << ' ' << i;
      cout << endl;
    }
  }

  return 0;
}
Problem p02283 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

struct Node {
  int key;
  Node *left;
  Node *right;
  Node() { left = right = NULL; }
};

class Tree {
  Node *root;

public:
  void insert(Node *n) {
    Node *y = NULL;
    Node *x = root;
    while (x != NULL) {
      y = x;
      if (n->key < x->key) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
    if (y == NULL) {
      root = n;
    } else if (n->key < y->key) {
      y->left = n;
    } else {
      y->right = n;
    }
  }

  void print() {
    print_inorder(root);
    printf("\n");
    print_preorder(root);
    printf("\n");
  }

  void print_preorder(Node *n) {
    if (n == NULL)
      return;
    printf(" %d", n->key);
    print_preorder(n->left);
    print_preorder(n->right);
  }
  void print_inorder(Node *n) {
    if (n == NULL)
      return;
    print_inorder(n->left);
    printf(" %d", n->key);
    print_inorder(n->right);
  }
};

int main() {
  int n;
  cin >> n;
  Tree t;
  for (int i = 0; i < n; i++) {
    string ope;
    cin >> ope;
    if (ope == "insert") {
      int k;
      cin >> k;
      Node *node = new Node();
      node->key = k;
      t.insert(node);
    } else if (ope == "print") {
      t.print();
    }
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

struct Node {
  int key;
  Node *left;
  Node *right;
  Node() { left = right = NULL; }
};

class Tree {
  Node *root = NULL;

public:
  void insert(Node *n) {
    Node *y = NULL;
    Node *x = root;
    while (x != NULL) {
      y = x;
      if (n->key < x->key) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
    if (y == NULL) {
      root = n;
    } else if (n->key < y->key) {
      y->left = n;
    } else {
      y->right = n;
    }
  }

  void print() {
    print_inorder(root);
    printf("\n");
    print_preorder(root);
    printf("\n");
  }

  void print_preorder(Node *n) {
    if (n == NULL)
      return;
    printf(" %d", n->key);
    print_preorder(n->left);
    print_preorder(n->right);
  }
  void print_inorder(Node *n) {
    if (n == NULL)
      return;
    print_inorder(n->left);
    printf(" %d", n->key);
    print_inorder(n->right);
  }
};

int main() {
  int n;
  cin >> n;
  Tree t;
  for (int i = 0; i < n; i++) {
    string ope;
    cin >> ope;
    if (ope == "insert") {
      int k;
      cin >> k;
      Node *node = new Node();
      node->key = k;
      t.insert(node);
    } else if (ope == "print") {
      t.print();
    }
  }
  return 0;
}