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

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

using namespace std;

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

// Tree::Tree(int k, Tree *l, Tree *r): key(k), left(l), right(r){}

void printTreeMid(Tree *tree) {
  // cout << "mid" << endl;
  if (tree == nullptr)
    return;
  printTreeMid(tree->left);
  printf(" %d", tree->key);
  printTreeMid(tree->right);
}

void printTreePro(Tree *tree) {
  if (tree == nullptr)
    return;
  printf(" %d", tree->key);
  printTreePro(tree->left);
  printTreePro(tree->right);
}

void insert(Tree *tree, int key) {
  Tree *y = nullptr;
  Tree *x = tree->right;
  while (x != nullptr) {
    // cout << "in while" << endl;
    y = x;
    if (x->key < key) {
      x = x->right;
    } else {
      x = x->left;
    }
  }
  Tree *z = (Tree *)calloc(1, sizeof(Tree));
  z->parent = y;
  z->key = key;
  z->left = nullptr;
  z->right = nullptr;
  if (z->parent == nullptr) {
    tree->right = z;
    return;
  }
  if (y->key < key) {
    y->right = z;
  } else {
    y->left = z;
  }
}

// int findNode(Tree *tree, int k) {
// 	if(tree == nullptr) {
// 		cout << "no" << endl;
// 		return 0;
// 	}
// 	if(tree->key == k) {
// 		cout << "yes" << endl;
// 		return 1;
// 	} else if(tree->key > k) {
// 		return findNode(tree->left, k);
// 	} else {
// 		return findNode(tree->right, k);
// 	}
// }

void findNode(Tree *tree, int k) {
  while (tree != nullptr) {
    if (tree->key == k) {
      cout << "yes" << endl;
      return;
    } else if (tree->key > k) {
      tree = tree->left;
    } else {
      tree = tree->right;
    }
  }
  cout << "no" << endl;
}

// 8:00
int main() {
  int n;
  cin >> n;
  Tree *NIL = new Tree();
  NIL->right = nullptr;
  for (size_t i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "insert") {
      int k;
      scanf("%d", &k);
      // if(tree != nullptr) {
      // 	cout << "root: " << tree->key;
      // }
      insert(NIL, k);
    } else if (s == "print") {
      printTreeMid(NIL->right);
      cout << endl;
      printTreePro(NIL->right);
      cout << endl;
    } else if (s == "find") {
      int k;
      scanf("%d", &k);
      findNode(NIL, k);
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define NIL -1
#define MAX 500001
using namespace std;

struct Node {
  int key;
  Node *parent, *left, *right;
  Node(int ikey) : key(ikey), parent(nullptr), left(nullptr), right(nullptr) {}
};

Node *root;

void insert(Node *parent, int key) {
  if (root == nullptr) {
    root = new Node(key);
    return;
  }
  if (key <= parent->key) {
    if (parent->left == nullptr) {
      parent->left = new Node(key);
      return;
    }
    insert(parent->left, key);
  } else {
    if (parent->right == nullptr) {
      parent->right = new Node(key);
      return;
    }
    insert(parent->right, key);
  }
}

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

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

bool find(Node *node, int key) {
  if (node == nullptr)
    return false;
  if (node->key == key)
    return true;
  return find(node->left, key) || find(node->right, key);
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    char s[20];
    scanf("%s", &s);
    if (strcmp(s, "insert") == 0) {
      int key;
      scanf("%d", &key);
      insert(root, key);
    } else if (strcmp(s, "find") == 0) {
      int key;
      scanf("%d", &key);
      if (find(root, key)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    } else if (strcmp(s, "print") == 0) {
      inorder(root);
      cout << "\\n";
      preorder(root);
      cout << "\\n";
    }
  }
}