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

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

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

Node *root = nullptr;

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

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

void insert(int key) {
  Node *node = (Node *)calloc(1, sizeof(Node));
  node->key = key;
  if (root == nullptr) {
    root = node;
    node->parent = nullptr;
    return;
  }

  Node *p = root;
  Node *parent = p->parent;
  while (p != nullptr) {
    if (p->key < key) {
      parent = p;
      p = p->right;
    } else if (p->key > key) {
      parent = p;
      p = p->left;
    } else {
      return;
    }
  }

  node->parent = parent;

  if (parent->key < key) {
    parent->right = node;
  } else if (parent->key > key) {
    parent->left = node;
  }
}

bool find(int key) {
  bool isFind = false;
  Node *p = root;
  while (p != nullptr) {
    if (p->key == key) {
      isFind = true;
      break;
    } else if (p->key > key) {
      p = p->left;
    } else {
      p = p->right;
    }
  }

  return isFind;
}

void keyDelete(int key) {
  Node *p = root;
  Node *parent = nullptr;

  while (p != nullptr) {
    if (p->key > key) {
      parent = p;
      p = p->left;
    } else if (p->key < key) {
      parent = p;
      p = p->right;
    } else if (p->key == key) {
      break;
    }
  }

  while (true) {
    if (p->left == nullptr && p->right == nullptr) {
      if (parent->key > key) {
        parent->left = nullptr;
      } else {
        parent->right = nullptr;
      }
      free(p);
      break;
    }
    if ((p->left != nullptr && p->right == nullptr) ||
        (p->left == nullptr && p->right != nullptr)) {
      if (p->left != nullptr) {
        if (parent->key < p->key) {
          parent->right = p->left;
        } else {
          parent->left = p->left;
        }
      } else if (p->right != nullptr) {
        if (parent->key < p->key) {
          parent->right = p->right;
        } else {
          parent->left = p->right;
        }
      }
      free(p);
      break;
    }

    if (p->left != nullptr && p->right != nullptr) {
      Node *_p = p->right;
      while (_p->left != nullptr) {
        _p = _p->left;
      }
      int tmpKey = _p->key;
      keyDelete(_p->key);
      p->key = tmpKey;
      break;
    }
  }
}

void print(Node *node) {
  middlePrint(node);
  printf("\\n");
  firstPrint(node);
  printf("\\n");
}

int main() {

  // cin.tie(0);
  // ios::sync_with_stdio(false);

  int m;
  scanf("%d", &m);
  string str;
  for (int i = 0; i < m; i++) {
    cin >> str;

    // insert
    if (str == "insert") {
      int key;
      scanf("%d", &key);
      insert(key);
    }
    // find
    else if (str[0] == 'f') {
      int key;
      scanf("%d", &key);
      if (find(key)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    } else if (str[0] == 'd') {
      int key;
      scanf("%d", &key);
      keyDelete(key);
    }
    // print
    else {
      print(root);
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

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

Node *root = nullptr;

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

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

void insert(int key) {
  Node *node = (Node *)calloc(1, sizeof(Node));
  node->key = key;
  if (root == nullptr) {
    root = node;
    node->parent = nullptr;
    return;
  }

  Node *p = root;
  Node *parent = p->parent;
  while (p != nullptr) {
    if (p->key < key) {
      parent = p;
      p = p->right;
    } else if (p->key > key) {
      parent = p;
      p = p->left;
    } else {
      return;
    }
  }

  node->parent = parent;

  if (parent->key < key) {
    parent->right = node;
  } else if (parent->key > key) {
    parent->left = node;
  }
}

bool find(int key) {
  bool isFind = false;
  Node *p = root;
  while (p != nullptr) {
    if (p->key == key) {
      isFind = true;
      break;
    } else if (p->key > key) {
      p = p->left;
    } else {
      p = p->right;
    }
  }

  return isFind;
}

void keyDelete(int key) {
  Node *p = root;
  Node *parent = nullptr;

  while (p != nullptr) {
    if (p->key > key) {
      parent = p;
      p = p->left;
    } else if (p->key < key) {
      parent = p;
      p = p->right;
    } else if (p->key == key) {
      break;
    }
  }

  while (true) {
    if (p->left == nullptr && p->right == nullptr) {
      if (parent->key > key) {
        parent->left = nullptr;
      } else {
        parent->right = nullptr;
      }
      free(p);
      break;
    }
    if ((p->left != nullptr && p->right == nullptr) ||
        (p->left == nullptr && p->right != nullptr)) {
      if (p->left != nullptr) {
        if (parent->key < p->key) {
          parent->right = p->left;
        } else {
          parent->left = p->left;
        }
      } else if (p->right != nullptr) {
        if (parent->key < p->key) {
          parent->right = p->right;
        } else {
          parent->left = p->right;
        }
      }
      free(p);
      break;
    }

    if (p->left != nullptr && p->right != nullptr) {
      Node *_p = p->right;
      while (_p->left != nullptr) {
        _p = _p->left;
      }
      p->key = _p->key;
      keyDelete(_p->key);
      break;
    }
  }
}

void print(Node *node) {
  middlePrint(node);
  printf("\\n");
  firstPrint(node);
  printf("\\n");
}

int main() {

  // cin.tie(0);
  // ios::sync_with_stdio(false);

  int m;
  scanf("%d", &m);
  string str;
  for (int i = 0; i < m; i++) {
    cin >> str;

    // insert
    if (str == "insert") {
      int key;
      scanf("%d", &key);
      insert(key);
    }
    // find
    else if (str[0] == 'f') {
      int key;
      scanf("%d", &key);
      if (find(key)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    } else if (str[0] == 'd') {
      int key;
      scanf("%d", &key);
      keyDelete(key);
    }
    // print
    else {
      print(root);
    }
  }

  return 0;
}