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

#include <iostream>
#include <memory>
#include <stdio.h>
#include <string.h>
using namespace std;

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

Node *root, *NIL;

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

  z = new 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 <iostream>
#include <memory>
#include <stdio.h>
#include <string.h>
using namespace std;

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

Node *NIL, *root;

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);
}

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

  z = new 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 < x->key) {
      x->left = z;
    } else {
      x->right = z;
    }
  }
}

bool Find(int k) {
  Node *x = root;
  while (x != NIL) {
    if (x->key == k)
      return true;
    if (x->key > k) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  return false;
}

int main() {
  int i, n, 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 == "find") {
      scanf("%d", &x);
      if (Find(x))
        printf("yes\\n");
      else
        printf("no\\n");
    } 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 <queue>

using namespace std;

#define NIL -1

struct Node {
  int parent, left, right, key;
};
Node tree[500001];
int treesize;
int root = NIL;

void in(int id) {
  if (id == NIL)
    return;
  cout << " " << tree[id].key;
  in(tree[id].left);
  in(tree[id].right);
}

void pre(int id) {
  if (id == NIL)
    return;
  pre(tree[id].left);
  cout << " " << tree[id].key;
  pre(tree[id].right);
}

void insert(int key) {
  int y = NIL;
  int x = root;
  while (x != NIL) {
    y = x;
    if (key < tree[x].key)
      x = tree[x].left;
    else
      x = tree[x].right;
  }
  tree[treesize].parent = y;

  if (y == NIL)
    root = treesize;
  else if (key < tree[y].key)
    tree[y].left = treesize;
  else
    tree[y].right = treesize;

  tree[treesize].left = NIL;
  tree[treesize].right = NIL;
  tree[treesize].key = key;

  treesize++;
}

int main(void) {
  int n;
  string str;

  cin >> n;

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

    if (str == "insert") {
      int key;
      cin >> key;
      insert(key);
    } else if (str == "print") {
      pre(root);
      cout << endl;
      in(root);
      cout << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;
#define NIL -1
struct Node {
  int right, left;
};
Node T[1000000];
int root = NIL;

void insert(int k) {
  T[k].left = NIL;
  T[k].right = NIL;

  int y = NIL;
  int x = root;

  while (x != NIL) {
    y = x;
    if (k < x)
      x = T[x].left;
    else
      x = T[x].right;
  }

  if (y == NIL)
    root = k;
  else if (k < y)
    T[y].left = k;
  else
    T[y].right = k;
}

void print1(int k) {
  if (k == NIL)
    return;
  print1(T[k].left);
  cout << " " << k;
  print1(T[k].right);
}

void print2(int k) {
  if (k == NIL)
    return;
  cout << " " << k;
  print2(T[k].left);
  print2(T[k].right);
}

int main() {
  int m, k;
  string str;

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> str;
    if (str == "insert") {
      cin >> k;
      insert(k);
    } else if (str == "print") {
      print1(root);
      cout << endl;
      print2(root);
      cout << endl;
    }
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct Node {
  int key;
  Node *parent, *left, *right;
};

Node *root, *NIL;

void Preorder(Node *pp) {
  if (pp == NIL)
    return;
  cout << " " << pp->key;
  Preorder(pp->left);
  Preorder(pp->right);
}
void Inorder(Node *in) {
  if (in == NIL)
    return;
  Inorder(in->left);
  cout << " " << in->key;
  Inorder(in->right);
}

void insert(int k) {
  Node *z;
  Node *y = NIL;  // x's parent
  Node *x = root; // T's root
  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;
  while (1) {
    if (x == NIL)
      break;
    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;
    }
  }
}
int main() {
  int n = 0, in = 0;
  int i = 0;
  string cel;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> cel;
    if (cel == "insert") {
      cin >> in;
      insert(in);
    } else if (cel == "print") {
      Inorder(root);
      cout << endl;
      Preorder(root);
      cout << endl;
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct Node {
  int key;
  Node *parent, *left, *right;
};

Node *root, *NIL;

void Preorder(Node *pp) {
  if (pp == NIL)
    return;
  cout << " " << pp->key;
  Preorder(pp->left);
  Preorder(pp->right);
}
void Inorder(Node *in) {
  if (in == NIL)
    return;
  Inorder(in->left);
  cout << " " << in->key;
  Inorder(in->right);
}

void insert(int k) {
  Node *z;
  Node *y = NIL;  // x's parent
  Node *x = root; // T's root
  z->key = k;
  z->left = NIL->left;
  z->right = NIL->right;
  while (1) {
    if (x == NIL)
      break;
    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;
  }
}
int main() {
  int n = 0, in = 0;
  int i = 0;
  string cel;
  cin >> n;
  for (i = 0; i < 8; i++) {
    cin >> cel;
    if (cel == "insert") {
      cin >> in;
      insert(in);
    } else if (cel == "print") {
      Inorder(root);
      cout << endl;
      Preorder(root);
      cout << endl;
    }
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<vector<int>> T;
vector<long> v;

void in(long k) {
  if (k == -1)
    return;
  else {
    in(T[k][0]);
    cout << " " << v[k];
    in(T[k][1]);
    return;
  }
}

void pre(long k) {
  if (k == -1)
    return;
  else {
    cout << " " << v[k];
    pre(T[k][0]);
    pre(T[k][1]);
    return;
  }
}

int main() {
  string s;
  long n = 0, t = 0;

  cin >> n;

  T.assign(n, {-1, -1});
  v.assign(n, -2000000001);

  for (long i = 0; i < n; i++) {
    cin >> s;

    if (s == "insert") {
      cin >> v[t];

      long y = -1, x = 0;

      while (x != -1) {
        int m = 1;
        y = x;
        if (v[t] < v[x])
          m = 0;
        x = T[x][m];
      }

      int s = 1;

      if (i > 0 && y != -1) {
        if (v[t] < v[y])
          s = 0;
        T[y][s] = t;
      }
      t++;
    } else if (s == "print") {
      in(0);
      cout << endl;
      pre(0);
      cout << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

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

const long nil = -200000001;
vector<vector<long>> T;

void in(long n) {
  long i = 0;
  if (n == nil)
    return;
  else {
    for (i = 0; T[i][0] != nil; i++) {
      if (T[i][0] == n)
        break;
    }
    in(T[i][1]);
    cout << " " << n;
    in(T[i][2]);
  }
}

void pre(long n) {
  long i = 0;
  if (n == nil)
    return;
  else {
    for (i = 0; T[i][0] != nil; i++) {
      if (T[i][0] == n)
        break;
    }
    cout << " " << n;
    pre(T[i][1]);
    pre(T[i][2]);
  }
}

int main() {
  string s;
  long n = 0, z = 0, t = 0;

  cin >> n;

  T.assign(n, {nil, nil, nil, nil});

  for (long i = 0; i < n; i++) {
    cin >> s;

    if (s == "insert") {
      cin >> z;

      long y = nil;
      long x = T[0][0];

      while (x != nil) {
        int m = 2;
        y = x;
        if (z < x)
          m = 1;
        for (long j = 0; j <= t; j++) {
          if (T[j][0] == x) {
            x = T[j][m];
            break;
          }
        }
      }

      int s = 2;

      if (y == nil)
        T[0][0] = z;
      else {
        T[t][0] = z;
        T[t][3] = y;
        if (z < y)
          s = 1;
        for (long j = 0; j <= t; j++) {
          if (T[j][0] == y) {
            T[j][s] = z;
            break;
          }
        }
      }
      t++;
    } else if (s == "print") {
      in(T[0][0]);
      cout << endl;
      pre(T[0][0]);
      cout << endl;
    }
  }

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

#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:

#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;
}
Problem p02283 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define N 4000000
#define NIL -1

int key[N];
int parent[N];
int leftc[N];
int rightc[N];

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

  y = NIL;
  x = root;

  while (x != NIL) {
    y = x;
    if (key[z] < key[x])
      x = leftc[x];
    else
      x = rightc[x];
  }

  parent[z] = y;

  if (y == NIL)
    root = z;

  else if (key[z] < key[y])
    leftc[y] = z;

  else
    rightc[y] = z;
}

void inorderprint(int now) {
  if (now != NIL) {
    inorderprint(leftc[now]);
    printf(" %d", key[now]);
    inorderprint(rightc[now]);
  }
}

void preorderprint(int now) {
  if (now != NIL) {
    printf(" %d", key[now]);
    preorderprint(leftc[now]);
    preorderprint(rightc[now]);
  }
}

int main() {

  int n, i, j, data;
  int z = 0;
  char str[10];

  for (i = 0; i < N; i++) {
    parent[i] = NIL;
    key[i] = NIL;
    rightc[i] = NIL;
    leftc[i] = NIL;
  }

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s", str);
    if (strcmp(str, "insert") == 0) {
      scanf("%d", &data);
      key[z] = data;
      insert(z);
      z++;
    } else if (strcmp(str, "print") == 0) {
      inorderprint(root);
      cout << endl;
      preorderprint(root);
      cout << endl;
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

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

#define N 4500000
#define NIL -1

int key[N];
int parent[N];
int leftc[N];
int rightc[N];

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

  y = NIL;
  x = root;

  while (x != NIL) {
    y = x;
    if (key[z] < key[x])
      x = leftc[x];
    else
      x = rightc[x];
  }

  parent[z] = y;

  if (y == NIL)
    root = z;

  else if (key[z] < key[y])
    leftc[y] = z;

  else
    rightc[y] = z;
}

void inorderprint(int now) {
  if (now != NIL) {
    inorderprint(leftc[now]);
    printf(" %d", key[now]);
    inorderprint(rightc[now]);
  }
}

void preorderprint(int now) {
  if (now != NIL) {
    printf(" %d", key[now]);
    preorderprint(leftc[now]);
    preorderprint(rightc[now]);
  }
}

int main() {

  int n, i, j, data;
  int z = 0;
  char str[10];

  for (i = 0; i < N; i++) {
    parent[i] = NIL;
    key[i] = NIL;
    rightc[i] = NIL;
    leftc[i] = NIL;
  }

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s", str);
    if (strcmp(str, "insert") == 0) {
      scanf("%d", &data);
      key[z] = data;
      insert(z);
      z++;
    } else if (strcmp(str, "print") == 0) {
      inorderprint(root);
      cout << endl;
      preorderprint(root);
      cout << endl;
    }
  }

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

#include <bits/stdc++.h>
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 <bits/stdc++.h>
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++) {
    scanf("%s", &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>

using namespace std;

#define NIL -2000000001

struct Node {
  int key;
  int left = NIL;
  int right = NIL;
};

Node tree[500000];

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

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

void insert(int s) {
  int y = NIL;
  int x = 0;

  while (x != NIL) {
    y = x;
    if (tree[s].key < tree[x].key) {
      x = tree[x].left;
    } else {
      x = tree[x].right;
    }
  }

  if (s == 0) {
    return;
  }

  if (tree[s].key < tree[y].key) {
    tree[y].left = s;
  } else {
    tree[y].right = s;
  }
}

int main() {
  int n, value;
  string str;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "insert") {
      cin >> value;
      tree[i].key = value;
      insert(i);
    } else if (str == "print") {
      inorder(0);
      cout << endl;
      preorder(0);
      cout << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define NIL -1

struct Node {
  int key;
  int parent = NIL;
  int left = NIL;
  int right = NIL;
};

int root = NIL;
vector<Node> tree;

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

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

void insert(int s) {
  int y = NIL;
  int x = root;

  while (x != NIL) {
    y = x;
    if (tree[s].key < tree[x].key) {
      x = tree[x].left;
    } else {
      x = tree[x].right;
    }
  }
  tree[s].parent = y;

  if (y == NIL) {
    root = s;
  } else if (tree[s].key < tree[y].key) {
    tree[y].left = s;
  } else {
    tree[y].right = s;
  }
}

int main() {
  int n, value;
  string str;
  Node node;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "insert") {
      cin >> value;
      node.key = value;
      tree.push_back(node);
      insert(i);
    } else {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
  }

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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
struct Node {
  int key;
  Node *p, *l, *r;
};
Node *root, *NIL;
void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;
  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->l = NIL;
  z->r = NIL;
  while (x != NIL) {
    y = x;
    if (z->key < x->key)
      x = x->l;
    else
      x = x->r;
  }
  z->p = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->l = z;
    } else {
      y->r = z;
    }
  }
}
void zx(Node *u) {
  if (u == NIL)
    return;
  zx(u->l);
  printf(" %d", u->key);
  zx(u->r);
}
void qx(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  qx(u->l);
  qx(u->r);
}
int main() {
  int n, k;
  scanf("%d", &n);
  string c;
  for (int i = 1; i <= n; i++) {
    cin >> c;
    if (c == "insert") {
      scanf("%d", &k);
      insert(k);
    } else if (c == "print") {
      zx(root);
      printf("\\n");
      qx(root);
      printf("\\n");
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
struct Node {
  int key;
  Node *p, *l, *r;
};
Node *root, *NIL;
void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;
  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->l = NIL;
  z->r = NIL;
  while (x != NIL) {
    y = x;
    if (z->key < x->key)
      x = x->l;
    else
      x = x->r;
  }
  x->p = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->l = z;
    } else {
      y->r = z;
    }
  }
}
void zx(Node *u) {
  if (u == NIL)
    return;
  zx(u->l);
  printf(" %d", u);
  zx(u->r);
}
void hx(Node *u) {
  if (u == NIL)
    return;
  hx(u->l);
  hx(u->r);
  printf(" %d", u);
}
int main() {
  int n, k;
  scanf("%d", &n);
  string c;
  for (int i = 1; i <= n; i++) {
    cin >> c;
    if (c == "insert") {
      scanf("%d", &k);
      insert(k);
    } else {
      zx(root);
      printf("\\n");
      hx(root);
      printf("\\n");
    }
  }
  return 0;
}