Problem p02285 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;

struct node {
  int key;
  node *left, *right, *p;
  node() {}
  node(int key) : key(key) { left = right = p = NULL; };
};
node *root;
void insert(node z) {
  node *y = NULL;
  node *x = root;
  while (x != NULL) {
    y = x;
    if (z.key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z.p = y;
  if (y == NULL)
    root = new node(z);
  else if (z.key < y->key)
    y->left = new node(z);
  else
    y->right = new node(z);
}
bool find(int z) {
  node *x = root;
  while (x != NULL) {
    if (z == x->key)
      return true;
    if (z < x->key)
      x = x->left;
    else
      x = x->right;
  }
  return false;
}
node *nxt(node *x) {
  if (x->left != NULL)
    return nxt(x->left);
  return x;
}
void del(node *x) {
  node *y = x->p;
  if (x->left == NULL && x->right == NULL) {
    if (y != NULL && y->left == x)
      y->left = NULL;
    if (y != NULL && y->right == x)
      y->right = NULL;
    delete (x);
    return;
  }
  if (x->left == NULL && x->right != NULL) {
    if (y != NULL && y->left == x)
      y->left = x->right;
    if (y != NULL && y->right == x)
      y->right = x->right;
    x->right->p = y;
    delete (x);
    return;
  }
  if (x->left != NULL && x->right == NULL) {
    if (y != NULL && y->left == x)
      y->left = x->left;
    if (y != NULL && y->right == x)
      y->right = x->left;
    x->left->p = y;
    delete (x);
    return;
  }
  node *z = nxt(x->right);
  x->key = z->key;
  del(z);
}
void del(int k) {
  node *x = root;
  while (x != NULL) {
    if (k == x->key)
      break;
    if (k < x->key)
      x = x->left;
    else
      x = x->right;
  }
  del(x);
}
void inorderTreeWalk(node *v) {
  if (v == NULL)
    return;
  inorderTreeWalk(v->left);
  cout << " " << v->key;
  inorderTreeWalk(v->right);
}
void preorderTreeWalk(node *v) {
  if (v == NULL)
    return;
  cout << " " << v->key;
  preorderTreeWalk(v->left);
  preorderTreeWalk(v->right);
}
signed main() {
  int n;
  cin >> n;
  root = NULL;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "print") {
      inorderTreeWalk(root);
      cout << endl;
      preorderTreeWalk(root);
      cout << endl;
      continue;
    }
    int x;
    cin >> x;
    if (s == "insert") {
      insert(node(x));
    }
    if (s == "find") {
      cout << (find(x) ? "yes" : "no") << endl;
    }
    if (s == "delete") {
      del(x);
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;

struct node {
  int key;
  node *left, *right, *p;
  node() {}
  node(int key) : key(key) { left = right = p = NULL; };
};
node *root;
void insert(node z) {
  node *y = NULL;
  node *x = root;
  while (x != NULL) {
    y = x;
    if (z.key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z.p = y; // parent
  if (y == NULL)
    root = new node(z);
  else if (z.key < y->key)
    y->left = new node(z);
  else
    y->right = new node(z);
}
bool find(int k) {
  node *x = root;
  while (x != NULL) {
    if (x->key == k)
      return true;
    if (k < x->key)
      x = x->left;
    else
      x = x->right;
  }
  return false;
}
node *nxt(node *x) {
  if (x->left != NULL)
    return nxt(x->left);
  return x;
}
// ノードxを消す
void del(node *x) {
  node *y = x->p;
  // xに子供がいないとき
  if ((x->left = NULL) && (x->right == NULL)) {
    if (y != NULL && y->left == x)
      y->left = NULL;
    if (y != NULL && y->right == x)
      y->right = NULL;
    delete (x);
    return;
  }
  if (x->left == NULL && x->right != NULL) {
    if (y != NULL && y->left == x)
      y->left = x->right;
    if (y != NULL && y->right == x)
      y->right = x->right;
    delete (x);
    return;
  }
  if (x->left != NULL && x->right == NULL) {
    if (y != NULL && y->left == x)
      y->left = x->left;
    if (y != NULL && y->right == x)
      y->right = x->left;
    delete (x);
    return;
  }
  node *z = nxt(x->right);
  x->key = z->key;
  del(z);
}
// 値がkのノードを見つける
void del(int k) {
  node *x = root;
  while (x != NULL) {
    if (k == x->key)
      break;
    if (k < x->key)
      x = x->left;
    else
      x = x->right;
  }
  del(x);
}
void inorderTreeWalk(node *v) {
  if (v == NULL)
    return;
  inorderTreeWalk(v->left);
  cout << " " << v->key;
  inorderTreeWalk(v->right);
}
void preorderTreeWalk(node *v) {
  if (v == NULL)
    return;
  cout << " " << v->key;
  preorderTreeWalk(v->left);
  preorderTreeWalk(v->right);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  root = NULL;
  FOR(i, 0, n) {
    string s;
    cin >> s;
    if (s == "insert") {
      int x;
      cin >> x;
      insert(node(x));
    } else if (s == "find") {
      int k;
      cin >> k;
      cout << (find(k) ? "yes" : "no") << endl;
    } else if (s == "delete") {
      int k;
      cin >> k;
      del(k);
    } else {
      inorderTreeWalk(root);
      cout << endl;
      preorderTreeWalk(root);
      cout << endl;
    }
  }
}