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>
#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;
    v.push_back(true);
    return;
  }
  if (exist == false) {
    find(u->left, i);
    find(u->right, i);
  }
}

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;
  vector<string> input;
  vector<int> insert_data;
  vector<int> find_data;
  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    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);
    }
  }

  int num = input.size();
  for (i = 0; i < num; i++) {
    if (input[i] == "insert") {
      insert(insert_data[0]);
      insert_data.erase(insert_data.begin());
    }
    if (input[i] == "print") {
      for (auto i : v) {
        if (i == true)
          cout << "yes" << endl;
        else
          cout << "no" << endl;
      }
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
    if (input[i] == "find") {
      exist = false;
      find(root, find_data[0]);
      find_data.erase(find_data.begin());
      if (exist == false)
        v.push_back(false);
    }
  }

  return 0;
}