alexjercan/bugnet huggingface dataset:
Problem p02282 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define bitcnt(x) __builtin_popcount(x)

// vector の中身を出力
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}

vector<int> pre, in, ans;

void rec(int lp, int rp, int li, int ri) {
  int root = pre[lp];
  int i = 0;
  while (in[li + i] != root)
    i++;
  if (i != 0)
    rec(lp + 1, lp + i, li, li + i - 1);
  if (li + i != ri)
    rec(lp + i + 1, rp, li + i + 1, ri);

  ans.pb(root);
}

void solve(void) {
  int N;
  cin >> N;
  pre.resize(N), in.resize(N);
  rep(N, i) cin >> pre[i];
  rep(N, i) cin >> in[i];
  rec(0, N - 1, 0, N - 1);
  rep(N, i) {
    if (i < N - 1)
      printf("%d ", ans[i]);
    else
      printf("%d\\n", ans[i]);
  }
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define bitcnt(x) __builtin_popcount(x)

// vector の中身を出力
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}

vector<int> pre, in, ans;

void rec(int lp, int rp, int li, int ri) {
  int root = pre[lp];
  int i = 0;
  while (in[li + i] != root)
    i++;
  if (i != 0)
    rec(lp + 1, lp + i, li, li + i - 1);
  if (li + i != ri)
    rec(lp + i + 1, rp, li + i + 1, ri);

  ans.pb(root);
}

void solve(void) {
  int N;
  cin >> N;
  pre.resize(N), in.resize(N);
  rep(N, i) cin >> pre[i];
  rep(N, i) cin >> in[i];
  rec(0, N - 1, 0, N - 1);
  for (int v : ans) {
    printf("%d ", v);
  }
  cout << endl;
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: 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 = 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);
}


Error: 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 = 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);
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

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

void fnInput(vector<int> &rvnPodr, vector<int> &rvnIodrSeq) {
  int nMaxSiz;

  cin >> nMaxSiz;
  rvnPodr.resize(nMaxSiz + 1);
  rvnIodrSeq.resize(nMaxSiz + 1);

  for (int i = 1; i < nMaxSiz + 1; i++)
    cin >> rvnPodr[i];

  int nNode;
  for (int i = 1; i < nMaxSiz + 1; i++) {
    cin >> nNode;
    rvnIodrSeq[nNode] = i;
  }
}

void fnPostOrder(const vector<int> &cnrvnPodr, const vector<int> &cnrvnIodrSeq,
                 int nLeft, int nRight) {
  static int stnx = 1;
  static int stnPrt = 0;

  if (nLeft == nRight)
    return;

  int nNode = cnrvnPodr[stnx++];
  int nMid = cnrvnIodrSeq[nNode];

  fnPostOrder(cnrvnPodr, cnrvnIodrSeq, nLeft, nMid);
  fnPostOrder(cnrvnPodr, cnrvnIodrSeq, nMid + 1, nRight);

  if (stnPrt++)
    cout << " ";
  cout << nNode;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<int> vnPodr, vnIodrSeq;
  fnInput(vnPodr, vnIodrSeq);

  fnPostOrder(vnPodr, vnIodrSeq, 1, vnPodr.size());
  cout << endl;
  return 0;
}

Error: -11

Fixed submission:

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

void fnInput(vector<int> &rvnPodr, vector<int> &rvnIodrSeq) {
  int nMaxSiz;

  cin >> nMaxSiz;
  rvnPodr.resize(nMaxSiz);
  rvnIodrSeq.resize(nMaxSiz + 1);

  for (int i = 0; i < nMaxSiz; i++)
    cin >> rvnPodr[i];

  int nNode;
  for (int i = 1; i < nMaxSiz + 1; i++) {
    cin >> nNode;
    rvnIodrSeq[nNode] = i;
  }
}

void fnPostOrder(const vector<int> &cnrvnPodr, const vector<int> &cnrvnIodrSeq,
                 int nLeft, int nRight) {
  static int stnx = 0;
  static int stnPrt = 0;

  if (nLeft == nRight)
    return;

  int nNode = cnrvnPodr[stnx++];
  int nMid = cnrvnIodrSeq[nNode];

  fnPostOrder(cnrvnPodr, cnrvnIodrSeq, nLeft, nMid);
  fnPostOrder(cnrvnPodr, cnrvnIodrSeq, nMid + 1, nRight);

  if (stnPrt++)
    cout << " ";
  cout << nNode;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<int> vnPodr, vnIodrSeq;
  fnInput(vnPodr, vnIodrSeq);

  fnPostOrder(vnPodr, vnIodrSeq, 0, vnPodr.size());
  cout << endl;
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: 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();
}

Error: -11

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 + 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();
}
Problem p02282 (C++) — buggy submission, verdict: 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;
}

Error: -11

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(1, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  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) {
    in.push_back(k);
  }
  solve();
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 500000

int n;

struct Tree {
  int key;
  struct Tree *parent, *left, *right;
};
struct Tree *NILL, *root;

void insert(struct Tree *z) {
  struct Tree *x, *y;
  y = NILL;
  x = root;
  while (x != NILL) {
    y = x;
    if (z->key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z->parent = y;
  if (y == NILL) {
    root = z;
  } else if (z->key < y->key)
    y->left = z;
  else
    y->right = z;
}

void mid(struct Tree *root) {
  if (root == NILL)
    return;
  mid(root->left);
  printf(" %d", root->key);
  mid(root->right);
}

void inorder(struct Tree *root) {
  if (root == NILL)
    return;
  printf(" %d", root->key);
  inorder(root->left);
  inorder(root->right);
}

void print() {
  struct Tree *r;
  r = root;
  mid(r);
  printf("\\n");
  inorder(r);
  printf("\\n");
}

int main() {
  struct Tree *t;
  scanf("%d", &n);
  int i;
  int key;
  char instruct[7];
  for (i = 0; i < n; i++) {
    scanf("%s", instruct);
    if (!strcmp(instruct, "insert")) {
      scanf("%d", &key);
      t = (struct Tree *)malloc(sizeof(struct Tree));
      t->key = key;
      t->right = NILL;
      t->left = NILL;
      // T[i]->key  = key;
      insert(t);
    } else
      print();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
  int key;
  struct node *right, *left, *parent;
};
struct node *root, *nil = NULL;

void insert(int k) {
  struct node *y = nil;
  struct node *x = root;
  struct node *z;
  z = (struct node *)malloc(sizeof(struct node));
  z->key = k;
  z->left = nil;
  z->right = nil;
  while (x != nil) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  if (y == nil) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}
void preorder(struct node *u) {
  if (u != nil) {
    printf(" %d", u->key);
    preorder(u->left);
    preorder(u->right);
  }
}

void inorder(struct node *u) {
  if (u != nil) {
    inorder(u->left);
    printf(" %d", u->key);
    inorder(u->right);
  }
}

int main() {
  int n, i, x;
  char *com;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s", com);
    printf("%s\\n", com);
    if (strcmp(com, "insert") == 0) {
      scanf("%d", &x);
      insert(x);
    } else if (strcmp(com, "print") == 0) {
      inorder(root);
      printf("\\n");
      preorder(root);
      printf("\\n");
    }
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

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

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }

  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}
void preorder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

int main() {
  int n, i, x;
  string com;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "print") {
      inorder(root);
      printf("\\n");
      preorder(root);
      printf("\\n");
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

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

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x->left;
    } else {
      x = x->right;
    }
  }

  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf("%d", u->key);
  inorder(u->right);
}
void preorder(Node *u) {
  if (u == NIL)
    return;
  printf("%d", u->key);
  preorder(u->left);
  preorder(u->right);
}

int main() {
  int n, i, x;
  string com;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "print") {
      inorder(root);
      printf("\\n");
      preorder(root);
      printf("\\n");
    }
  }

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

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

#define NIL 2000000001
#define MAX 500001

class Node {
public:
  int key, l = MAX, r = MAX;
};
vector<Node> nodes(MAX);
int p = 0, root = 0;

void insert(int key) {
  nodes.at(p).key = key;
  if (p == 0) {
    p++;
    return;
  }
  int *cur = &root;
  while (MAX != *cur) {
    if (nodes.at(*cur).key > key)
      cur = &nodes.at(*cur).l;
    else
      cur = &nodes.at(*cur).r;
  }
  *cur = p++;
}

void inorder(int cur) {
  if (MAX == cur)
    return;
  inorder(nodes.at(cur).l);
  cout << " " << nodes.at(cur).key;
  inorder(nodes.at(cur).r);
}

void preorder(int cur) {
  if (MAX == cur)
    return;
  cout << " " << nodes.at(cur).key;
  preorder(nodes.at(cur).l);
  preorder(nodes.at(cur).r);
}

int main() {
  int n, key;
  string command;
  cin >> n;
  while (n--) {
    cin >> command;
    if ("insert" == command) {
      cin >> key;
      insert(key);
    }
    if ("print" == command) {
      inorder(0);
      cout << endl;
      preorder(0);
      //            for (int i = 0; i < p; i++) {
      //                cout << " " << nodes.at(i).key;
      //            }
      cout << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

class Node {
public:
  Node *l, *r;
  int key;
};

void insert(Node **root, int key) {
  Node **cur = root, *node = (Node *)malloc(sizeof(Node));
  node->key = key;
  while (NULL != *cur) {
    if (node->key < (*cur)->key)
      cur = &((*cur)->l);
    else
      cur = &((*cur)->r);
  }
  *cur = node;
  return;
}

void inorder(Node *cur) {
  if (cur == NULL)
    return;
  inorder(cur->l);
  cout << " " << cur->key;
  inorder(cur->r);
}

void preorder(Node *cur) {
  if (cur == NULL)
    return;
  cout << " " << cur->key;
  preorder(cur->l);
  preorder(cur->r);
  free(cur);
}

int main() {
  Node *root = NULL;
  int n, key;
  string command;
  cin >> n;
  while (n--) {
    cin >> command;
    if ("insert" == command) {
      cin >> key;
      insert(&root, key);
    }
    if ("print" == command) {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

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

Node *root, *NIL;
void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  if (y == NIL) {
    root = z;
  } else if (z->key < y->key) {
    y->left = z;
  } else {
    y->right = z;
  }
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preorder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

int main() {
  int n, i, x;
  string com;
  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "print") {
      inorder(root);
      printf("\\n");
      preorder(root);
      printf("\\n");
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

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

Node *root, *NIL;
void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  if (z->key < y->key) {
    y->left = z;
  } else {
    y->right = z;
  }
}
void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preorder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

int main() {
  int n, i, x;
  string com;
  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "print") {
      inorder(root);
      printf("\\n");
      preorder(root);
      printf("\\n");
    }
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

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

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

Node *root;

void Preorder(Node *root) {
  if (root == nullptr) {
    return;
  }
  std::cout << " " << root->key;
  Preorder(root->l);
  Preorder(root->r);
}

void Inorder(Node *root) {
  if (root == nullptr) {
    return;
  }
  Inorder(root->l);
  std::cout << " " << root->key;
  Inorder(root->r);
}

void Insert(Node *tgt) {
  Node *p = nullptr, *c = root;
  while (c != nullptr) {
    p = c;
    if (tgt->key < c->key) {
      c = c->l;
    } else {
      c = c->r;
    }
  }
  tgt->p = p;
  if (p == nullptr) {
    root = tgt;
  } else if (tgt->key < p->key) {
    p->l = tgt;
  } else {
    p->r = tgt;
  }
}

int main() {
  int n;
  std::cin >> n;
  std::vector<Node> T;
  T.reserve(n * 2);
  int c = 0;
  for (int i = 0; i < n; i++) {
    std::string s;
    int key;
    std::cin >> s;
    if (s == "print") {
      Inorder(root);
      std::cout << std::endl;
      Preorder(root);
      std::cout << std::endl;
      continue;
    }
    std::cin >> key;
    T.emplace_back(Node{0, nullptr, nullptr, nullptr});
    T[c].key = key;
    Insert(&T[c]);
    c++;
  }
  return 0;
}


Error: 0

Fixed submission:

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

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

Node *root;

void Preorder(Node *root) {
  if (root == nullptr) {
    return;
  }
  std::cout << " " << root->key;
  Preorder(root->l);
  Preorder(root->r);
}

void Inorder(Node *root) {
  if (root == nullptr) {
    return;
  }
  Inorder(root->l);
  std::cout << " " << root->key;
  Inorder(root->r);
}

void Insert(Node *tgt) {
  Node *p, *c = root;
  while (c != nullptr) {
    p = c;
    if (tgt->key < c->key) {
      c = c->l;
    } else {
      c = c->r;
    }
  }
  tgt->p = p;
  if (p == nullptr) {
    root = tgt;
  } else if (tgt->key < p->key) {
    p->l = tgt;
  } else {
    p->r = tgt;
  }
}

int main() {
  int n;
  std::cin >> n;
  std::vector<Node> T;
  T.reserve(n * 2);
  for (int i = 0; i < n; i++) {
    std::string s;
    int key;
    std::cin >> s;
    if (s == "print") {
      Inorder(root);
      std::cout << std::endl;
      Preorder(root);
      std::cout << std::endl;
      continue;
    }
    std::cin >> key;
    T.emplace_back(Node{0, nullptr, nullptr, nullptr});
    T[i].key = key;
    Insert(&T[i]);
  }
  return 0;
}