Problem p02284 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

struct node {
  int key;
  node *left;
  node *right;
  node(int k) : key(k), left(nullptr), right(nullptr) {}
};

void insert(node *to, node *v) {
  if (v->key < to->key) {
    if (to->left != nullptr) {
      insert(to->left, v);
    } else {
      to->left = v;
    }
  } else {
    if (to->right != nullptr) {
      insert(to->right, v);
    } else {
      to->right = v;
    }
  }
}

bool find(node *to, node *v) {
  if (to->key == v->key) {
    return true;
  }
  bool ret = false;
  if (v->key < to->key) {
    if (to->left != nullptr) {
      ret |= find(to->left, v);
    }
  } else {
    if (to->right != nullptr) {
      ret |= find(to->right, v);
    }
  }
  return ret;
}

void print_first(node *target) {
  cout << " " << target->key;
  if (target->left != nullptr)
    print_first(target->left);
  if (target->right != nullptr)
    print_first(target->right);
}
void print_mid(node *target) {
  if (target->left != nullptr)
    print_mid(target->left);
  cout << " " << target->key;
  if (target->right != nullptr)
    print_mid(target->right);
}

void deletetree(node *target) {
  if (target->left != nullptr)
    deletetree(target->left);
  if (target->right != nullptr)
    deletetree(target->right);
  delete target;
}

int main() {
  int N;
  int num;
  string inst;

  cin >> N;

  cin >> inst;
  cin >> num;
  N--;

  node *root = new node(num);
  for (int i = 0; i < N; ++i) {
    cin >> inst;
    if (inst == "insert") {
      cin >> num;
      node *v = new node(num);
      insert(root, v);

    } else if (inst == "print") {
      print_mid(root);
      cout << endl;
      print_first(root);
      cout << endl;
    } else if (inst == "find") {
      cin >> num;
      node *v = new node(num);
      if (find(root, v)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }
  deletetree(root);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

struct node {
  int key;
  node *left;
  node *right;
  node(int k) : key(k), left(nullptr), right(nullptr) {}
};

void insert(node *to, node *v) {
  if (v->key < to->key) {
    if (to->left != nullptr) {
      insert(to->left, v);
    } else {
      to->left = v;
    }
  } else {
    if (to->right != nullptr) {
      insert(to->right, v);
    } else {
      to->right = v;
    }
  }
}

bool find(node *to, node *v) {
  if (to->key == v->key) {
    return true;
  }
  bool ret = false;
  if (to->left != nullptr) {
    ret |= find(to->left, v);
  }
  if (to->right != nullptr) {
    ret |= find(to->right, v);
  }
  return ret;
}

void print_first(node *target) {
  cout << " " << target->key;
  if (target->left != nullptr)
    print_first(target->left);
  if (target->right != nullptr)
    print_first(target->right);
}
void print_mid(node *target) {
  if (target->left != nullptr)
    print_mid(target->left);
  cout << " " << target->key;
  if (target->right != nullptr)
    print_mid(target->right);
}

void deletetree(node *target) {
  if (target->left != nullptr)
    deletetree(target->left);
  if (target->right != nullptr)
    deletetree(target->right);
  delete target;
}

int main() {
  int N;
  int num;
  string inst;

  cin >> N;

  cin >> inst;
  cin >> num;
  N--;

  node *root = new node(num);
  for (int i = 0; i < N; ++i) {
    cin >> inst;
    if (inst == "insert") {
      cin >> num;
      node *v = new node(num);
      insert(root, v);

    } else if (inst == "print") {
      print_mid(root);
      cout << endl;
      print_first(root);
      cout << endl;
    } else if (inst == "find") {
      cin >> num;
      node *v = new node(num);
      if (find(root, v)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }
  deletetree(root);
}