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

#include <cstdio>

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

enum order { pre, in };

void rec(node *t, order o) {
  if (!t)
    return;
  if (o == pre)
    printf(" %d", t->key);
  rec(t->left, o);
  if (o == in)
    printf(" %d", t->key);
  rec(t->right, o);
}

int main() {
  node *T(nullptr);
  size_t m;
  scanf("%zu\\n", &m);
  for (size_t i = 0; i < m; i++) {
    char s[8];
    scanf("%s", s);
    switch (s[0]) {
    case 'i': {
      int k;
      scanf("%d", &k);
      node *x = T, *y = nullptr, *z = new node(k);
      while (x) {
        y = x;
        x = z->key < y->key ? y->left : y->right;
      }
      (y ? z->key < y->key ? y->left : y->right : T) = z;
    } break;
    case 'f': {
      int k;
      scanf("%d", &k);
      bool found = false;
      for (auto x = T; x; x = k < x->key ? x->left : x->right)
        if (x->key == k) {
          found = true;
          break;
        }
      puts(found ? "yes" : "no");
    } break;
    case 'd': {
      int k;
      scanf("%d", &k);
      node *x = T, *y = nullptr;
      while (x) {
        if (x->key == k)
          break;
        y = x;
        x = k < x->key ? x->left : x->right;
      }
      if (x->key == k) {
        if (x->left && x->right) {
          auto z = x->right, w = x;
          while (z->left) {
            w = z;
            z = z->left;
          }
          (z->key < w->key ? w->left : w->right) = z->right;
          z->right = nullptr;
          x->key = z->key;
          delete z;
        } else if (x->left) {
          (y ? x->key < y->key ? y->left : y->right : T) = x->left;
          x->left = nullptr;
          delete x;
        } else if (x->right) {
          (y ? x->key < y->key ? y->left : y->right : T) = x->right;
          x->right = nullptr;
          delete x;
        } else {
          (y ? x->key < y->key ? y->left : y->right : T) = nullptr;
          delete x;
        }
      }
    } break;
    default:
      rec(T, in);
      puts("");
      rec(T, pre);
      puts("");
      break;
    }
  }
  delete T;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

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

enum order { pre, in };

void rec(node *t, order o) {
  if (!t)
    return;
  if (o == pre)
    printf(" %d", t->key);
  rec(t->left, o);
  if (o == in)
    printf(" %d", t->key);
  rec(t->right, o);
}

int main() {
  node *T(nullptr);
  size_t m;
  scanf("%zu\\n", &m);
  for (size_t i = 0; i < m; i++) {
    char s[8];
    scanf("%s", s);
    switch (s[0]) {
    case 'i': {
      int k;
      scanf("%d", &k);
      node *x = T, *y = nullptr, *z = new node(k);
      while (x) {
        y = x;
        x = z->key < y->key ? y->left : y->right;
      }
      (y ? z->key < y->key ? y->left : y->right : T) = z;
    } break;
    case 'f': {
      int k;
      scanf("%d", &k);
      bool found = false;
      for (auto x = T; x; x = k < x->key ? x->left : x->right)
        if (x->key == k) {
          found = true;
          break;
        }
      puts(found ? "yes" : "no");
    } break;
    case 'd': {
      int k;
      scanf("%d", &k);
      node *x = T, *y = nullptr;
      while (x) {
        if (x->key == k)
          break;
        y = x;
        x = k < x->key ? x->left : x->right;
      }
      if (x->key == k) {
        if (x->left && x->right) {
          auto z = x->right, w = x;
          while (z->left)
            w = z;
          (z->key < w->key ? w->left : w->right) = z->right;
          z->right = nullptr;
          x->key = z->key;
          delete z;
        } else if (x->left) {
          (y ? x->key < y->key ? y->left : y->right : T) = x->left;
          x->left = nullptr;
          delete x;
        } else if (x->right) {
          (y ? x->key < y->key ? y->left : y->right : T) = x->right;
          x->right = nullptr;
          delete x;
        } else {
          (y ? x->key < y->key ? y->left : y->right : T) = nullptr;
          delete x;
        }
      }
    } break;
    default:
      rec(T, in);
      puts("");
      rec(T, pre);
      puts("");
      break;
    }
  }
  delete T;
  return 0;
}