Problem p02284 (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;
    }
  }
}

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

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, x;
  string com;

  scanf("%d", &n);

  for (int 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");
    } else if (com == "find") {
      scanf("%d", &x);
      Node *nd = find(root, x);
      if (nd != NIL)
        printf("yes\\n");
      else
        printf("no\\n");
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

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

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

Node *root, *NIL;
bool exist = false;
vector<bool> v;

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 find(Node *u, int i) {

  if (u == NIL) {
    return;
  }
  if (u->key == i) {
    exist = true;
    printf("yes\\n");
    return;
  }
  if (i < u->key) {
    find(u->left, i);
  } else {
    find(u->right, i);
  }
}

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

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

int main() {
  int n, i, x;
  string com;
  vector<string> input;
  vector<int> insert_data;
  vector<int> find_data;
  scanf("%d", &n);

  i = 0;
  while (i < n) {
    cin >> com;
    if (com == "insert") {
      input.push_back("insert");
      scanf("%d", &x);
      insert_data.push_back(x);
    } else if (com == "print") {
      input.push_back("print");
    } else if (com == "find") {
      input.push_back("find");
      scanf("%d", &x);
      find_data.push_back(x);
    }
    i++;
  }

  int num = input.size();
  i = 0;
  while (i < num) {
    if (input[i] == "insert") {
      insert(insert_data[0]);
      insert_data.erase(insert_data.begin());
    }
    if (input[i] == "print") {
      inorder(root);
      cout << "\\n";
      preorder(root);
      cout << "\\n";
    }
    if (input[i] == "find") {
      exist = false;
      find(root, find_data[0]);
      if (exist == false)
        printf("no\\n");
      find_data.erase(find_data.begin());
    }
    i++;
  }

  return 0;
}