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

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

struct Node {
  int key;
  Node *parent, *left, *right;
};
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 (int i = 0; i < n; i++) {
    cin >> com;
    /*	if(com[0]=='f')
            {
                    scanf("%d",&x);
                    find(root,x);
            }*/
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "print") {
      inorder(root);
      printf("\\n");
      preorder(root);
      printf("\\n");
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
const int max1 = -2000000001;
int k;
struct kz {
  int node = max1;
  int left = -1;
  int right = -1;
} Tree[500010];
void mmp(int x) {
  if (k > Tree[x].node) {
    if (Tree[x].right == -1) {
      Tree[x * 2 + 1].node = k;
      Tree[x].right = 0;
      return;
    } else
      mmp(2 * x + 1);
  } else {
    if (Tree[x].left == -1) {
      Tree[x * 2].node = k;
      Tree[x].left = 0;
      return;
    } else
      mmp(2 * x);
  }
}
void mmp2(int x) {
  if (Tree[x].node == max1)
    return;
  printf(" %d", Tree[x].node);
  mmp2(2 * x);
  mmp2(2 * x + 1);
}
void mmp3(int x) {
  if (Tree[x].node == max1)
    return;
  mmp3(2 * x);
  printf(" %d", Tree[x].node);
  mmp3(2 * x + 1);
}
int main() {
  int n;
  char s[20];
  scanf("%d", &n);
  scanf("%s %d", s, &k);
  Tree[1].node = k;
  for (int i = 1; i < n; i++) {
    scanf("%s", s);
    if (s[0] == 'p') {
      mmp3(1);
      printf("\\n");
      mmp2(1);
      printf("\\n");
      continue;
    }
    scanf("%d", &k);
    mmp(1);
  }
  return 0;
}
Problem p02284 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX 100
// #define NIL -1

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

Node *root, *NIL;
int n;

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

  z->key = val;
  z->left = NIL;
  z->right = NIL;

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

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

Node *find(Node *x, int val) {
  while (x != NIL) {
    if (x->key == val)
      break;
    if (x->key > val) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  return x;
}

void preorder(Node *u) {
  if (u == NIL)
    return;

  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

void inorder(Node *u) {
  if (u == NIL)
    return;

  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void print() {
  inorder(root);
  printf("\\n");
  preorder(root);
  printf("\\n");
}

int main() {
  int val;
  char moji[10];

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", moji);
    if (moji[0] == 'i') {
      scanf("%d", &val);
      insert(val);
    } else if (moji[0] == 'f') {
      scanf("%d", &val);
      Node *t = find(root, val);
      if (t != NIL)
        printf("yes\\n");
      else
        printf("no\\n");
    } else {
      print();
    }
  }

  return 0;
}

Error: -11

Fixed submission:

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

#define MAX 100
// #define NIL -1

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

Node *root, *NIL;
int n;

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

  z->key = val;
  z->left = NIL;
  z->right = NIL;

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

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

void find(Node *x, int val) {
  while (x->key != val && x != NIL) {
    if (x->key > val) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  if (x->key == val) {
    printf("yes\\n");
  } else {
    printf("no\\n");
  }
}

void preorder(Node *u) {
  if (u == NIL)
    return;

  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

void inorder(Node *u) {
  if (u == NIL)
    return;

  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void print() {
  inorder(root);
  printf("\\n");
  preorder(root);
  printf("\\n");
}

int main() {
  int val;
  char moji[10];

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", moji);
    if (moji[0] == 'i') {
      scanf("%d", &val);
      insert(val);
    } else if (moji[0] == 'f') {
      scanf("%d", &val);
      find(root, val);
    } else {
      print();
    }
  }

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

#include <algorithm>
#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;
    } else if (s == "find") {
      long w = 0;
      cin >> w;

      long x = 0;

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

      if (x != -1)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#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;
    } else if (s == "find") {
      long w = 0;
      cin >> w;
      std::vector<long>::iterator itr = find(v.begin(), v.end(), w);
      if (itr != v.end())
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02284 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

class Node {
public:
  Node *l, *r, *p;
  int key;
  Node() { l = r = p = NULL; }
};
Node *root = NULL;

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

Node *find(int key) {
  Node *cur = root;
  while (NULL != cur) {
    if (cur->key == key)
      break;
    if (key < cur->key)
      cur = cur->l;
    else
      cur = cur->r;
  }
  return cur;
}

Node *get_least(Node *r) {
  if (r == NULL)
    return NULL;
  while (r->l)
    r = r->l;
  return r;
}

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

int main() {
  int n, key;
  string command;
  cin >> n;
  while (n--) {
    cin >> command;
    if ("insert" == command) {
      cin >> key;
      insert(key);
    } else if ("find" == command) {
      cin >> key;
      cout << (find(key) ? "yes" : "no") << endl;
    } else if ("print" == command) {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    } else
      printf("ERROR: %d\\n", __LINE__);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

class Node {
public:
  Node *l, *r, *p;
  int key;
  Node() { l = r = p = NULL; }
};

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

Node *find(Node *root, int key) {
  Node *cur = root;
  while (NULL != cur) {
    if (cur->key == key)
      break;
    if (key < cur->key)
      cur = cur->l;
    else
      cur = cur->r;
  }
  return cur;
}

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 ("find" == command) {
      cin >> key;
      cout << (find(root, key) ? "yes" : "no") << endl;
    }
    if ("print" == command) {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
  }
  return 0;
}
Problem p02284 (C++) — buggy submission, verdict: Runtime Error

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

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

int n;
node *N;

void tree_insert(node *z) {
  node *y = NULL;
  node *x = N;
  while (x != NULL) {
    y = x;
    if (x->key > z->key)
      x = x->l;
    else if (x->key < z->key)
      x = x->r;
  }
  z->p = y;
  if (y == NULL)
    N = z;
  else if (y->key < z->key)
    y->r = z;
  else
    y->l = z;
}

void tree_find(int num) {
  node *x = N;
  while (x != NULL) {
    if (x->key == num) {
      cout << "yes" << endl;
      return;
    }
    if (x->key > num)
      x = x->l;
    else
      x = x->r;
  }
  cout << "no" << endl;
}

void in(node *x) {
  if (x->l != NULL)
    in(x->l);
  cout << " " << x->key;
  if (x->r != NULL)
    in(x->r);
}

void pre(node *x) {
  cout << " " << x->key;
  if (x->l != NULL)
    pre(x->l);
  if (x->r != NULL)
    pre(x->r);
}

void print() {
  node *x = N;
  in(x);
  cout << endl;
  pre(x);
  cout << endl;
}

int main() {
  N = NULL;
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "insert") {
      int num;
      cin >> num;
      node *z = new node();
      z->key = num;
      z->l = z->r = z->p = NULL;
      tree_insert(z);
    }
    if (s == "find") {
      int num;
      cin >> num;
      tree_find(num);
    }
    if (s == "print") {
      print();
    }
  }
  return 0;
}

Error: -11

Fixed submission:

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

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

int n;
node *N;

void tree_insert(node *z) {
  node *y = NULL;
  node *x = N;
  while (x != NULL) {
    y = x;
    if (x->key > z->key)
      x = x->l;
    else if (x->key < z->key)
      x = x->r;
  }
  z->p = y;
  if (y == NULL)
    N = z;
  else if (y->key < z->key)
    y->r = z;
  else
    y->l = z;
}

void tree_find(int num) {
  node *x = N;
  while (x != NULL) {
    if (x->key == num) {
      cout << "yes" << endl;
      return;
    }
    if (x->key > num)
      x = x->l;
    if (x->key < num)
      x = x->r;
  }
  cout << "no" << endl;
}

void in(node *x) {
  if (x->l != NULL)
    in(x->l);
  cout << " " << x->key;
  if (x->r != NULL)
    in(x->r);
}

void pre(node *x) {
  cout << " " << x->key;
  if (x->l != NULL)
    pre(x->l);
  if (x->r != NULL)
    pre(x->r);
}

void print() {
  node *x = N;
  in(x);
  cout << endl;
  pre(x);
  cout << endl;
}

int main() {
  N = NULL;
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "insert") {
      int num;
      cin >> num;
      node *z = new node();
      z->key = num;
      z->l = z->r = z->p = NULL;
      tree_insert(z);
    }
    if (s == "find") {
      int num;
      cin >> num;
      tree_find(num);
    }
    if (s == "print") {
      print();
    }
  }
  return 0;
}
Problem p02284 (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 = 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);
  cout << " " << u->key;
  inorder(u->right);
}

void preorder(Node *u) {
  if (u == NIL)
    return;
  cout << " " << u->key;
  preorder(u->left);
  preorder(u->right);
}

Node *find(Node *u, int k) {
  while (u != NIL && k != u->key) {
    if (k < u->key)
      u = u->left;
    else
      u = u->right;
  }
  return u;
}
int main() {
  int n, x;
  string com;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    cin >> com;
    if (com == "insert") {
      cin >> x;
      insert(x);
    } else if (com == "print") {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    } else if (com == "find") {
      cin >> x;
      Node *t = find(root, x);
      if (t != NIL)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

const long NIL = -1;
struct Node {
  long left;
  long right;
};

Node T[500000];
long root = NIL;

void printInOrder(long i) {
  if (i == NIL) {
    return;
  }
  printInOrder(T[i].left);
  printf(" %d", i);
  printInOrder(T[i].right);
}

void printPreOrder(long i) {
  if (i == NIL) {
    return;
  }
  printf(" %d", i);
  printPreOrder(T[i].left);
  printPreOrder(T[i].right);
}

bool find(long i) {
  if (T[i].right == 0 && T[i].left == 0)
    return false;
  else
    return true;
}

void insert(long i) {
  //??°???????????\\???????????????????????????NIL
  T[i].left = T[i].right = NIL;
  //??????????????£???
  if (root == NIL) {
    root = i;
    return;
  }
  // x??§dig??????
  long y;
  long x = root;
  while (x != NIL) {
    y = x;
    if (i < x) {
      x = T[x].left;
    } else {
      x = T[x].right;
    }
  }
  if (i < y) {
    T[y].left = i;
  } else {
    T[y].right = i;
  }
}

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

  string com;
  long key;
  for (long i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      cin >> key;
      insert(key);
    } else if (com == "find") {
      cin >> key;
      if (find(key))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    } else {
      printInOrder(root);
      printf("\\n");
      printPreOrder(root);
      printf("\\n");
    }
  }
  return 0;
}
Problem p02284 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
using namespace std;

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

Node *NIL = NULL;

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

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

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

void insert(Node *&T, Node *z) {
  Node *y = NIL;
  Node *x = T;
  while (x != NIL) {
    y = x;
    if (z->key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z->parent = y;

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

bool find(Node *T, int key) {
  Node *x = T;
  if (x == NIL)
    return false;
  if (x->key == key)
    return true;
  else if (key < x->key)
    return find(x->left, key);
  else
    return find(x->right, key);
}

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

  Node *T = NIL;
  for (int i = 0; i < n; ++i) {
    char cmd[20];
    int v;
    scanf("%s", cmd);
    if (cmd[0] == 'i') {
      scanf("%d", &v);
      Node *p = new Node;
      p->key = v;
      p->left = NIL;
      p->right = NIL;
      insert(T, p);
    } else if (cmd[0] == 'f') {
      scanf("%d", &v);
      printf("%s\\n", find(T, v) ? "yes" : "no");
    } else if (cmd[0] == 'p') {
      inorder(T);
      printf("\\n");
      preorder(T);
      printf("\\n");
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
using namespace std;

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

Node *NIL = NULL;

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

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

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

void insert(Node *&T, Node *z) {
  Node *y = NIL;
  Node *x = T;
  while (x != NIL) {
    y = x;
    if (z->key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z->parent = y;

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

bool find(Node *T, int key) {
  Node *x = T;
  if (x == NIL)
    return false;
  if (x->key == key)
    return true;
  else if (key < x->key)
    return find(x->left, key);
  else
    return find(x->right, key);
}

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

  Node *T = NIL;
  for (int i = 0; i < n; ++i) {
    char cmd[20];
    int v;
    scanf("%s", cmd);
    if (cmd[0] == 'i') {
      scanf("%d", &v);
      Node *p = new Node;
      p->key = v;
      insert(T, p);
    } else if (cmd[0] == 'f') {
      scanf("%d", &v);
      printf("%s\\n", find(T, v) ? "yes" : "no");
    } else if (cmd[0] == 'p') {
      inorder(T);
      printf("\\n");
      preorder(T);
      printf("\\n");
    }
  }

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

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

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

Node *root, *NIL;

Node *find(Node *root, int key) {
  while (root != NIL && key != root->key) {
    if (key < root->key)
      root = root->left;
    else
      root = root->right;
  }
  return root;
}

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

  z = (Node *)malloc(sizeof(Node));
  z->key = key;
  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 *root) {
  if (root == NIL)
    return;
  inorder(root->left);
  cout << " " << root->key;
  inorder(root->right);
}

void preorder(Node *root) {
  if (root == NIL)
    return;
  cout << " " << root->key;
  preorder(root->left);
  preorder(root->right);
}

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

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "find") {
      cin >> key;
      Node *t = find(root, key);
      if (t != NIL)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    } else if (str == "insert") {
      cin >> key;
      insert(key);
    } else if (str == "print") {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

bool find(int id) {
  if (!treesize)
    return false;
  else {
    for (int i = 0; i < treesize; ++i) {
      if (tree[i].key == id)
        return true;
    }
    return false;
  }
}

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 == "find") {
      int value;
      cin >> value;
      if (find(value))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    } else if (str == "print") {
      pre(root);
      cout << endl;
      in(root);
      cout << endl;
    }
  }

  return 0;
}