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

#include <cstdio>
#include <cstdlib>
#pragma warning(disable : 4996)

struct Node {
  int k;
  Node *p, *l, *r;
};

Node *root = NULL;
int m, k;
char com[10];

void insert(int k) {
  Node *x = root;
  Node *y = NULL;
  Node *z = (Node *)malloc(sizeof(Node));
  z->k = k;
  z->l = NULL;
  z->r = NULL;

  while (x != NULL) {
    y = x;
    if (z->k < x->k) {
      x = x->l;
    } else {
      x = x->r;
    }
  }

  z->p = y;
  if (y == NULL) {
    root = z;
  } else {
    if (z->k < y->k) {
      y->l = z;
    } else {
      y->r = z;
    }
  }
}

Node *find(int k) {
  Node *node = root;
  while (node != NULL && node->k != k) {
    if (k < node->k)
      node = node->l;
    else
      node = node->r;
  }
  return node;
}

Node *treeMinimum(Node *node) {
  while (node->l != NULL)
    node = node->l;
  return node;
}

Node *treeSuccessor(Node *x) {
  if (x->r != NULL)
    return treeMinimum(x->r);
  Node *y = x->p;
  while (y != NULL && x == y->r) {
    x = y;
    y = y->p;
  }
  return y;
}

void deleteNode(Node *z) {
  Node *x;
  Node *y;

  if (z->l == NULL || z->r == NULL)
    y = z;
  else
    y = treeSuccessor(z);
  if (y->l != NULL) {
    x = y->l;
  } else {
    x = y->r;
  }

  if (x != NULL) {
    x->p = y->p;
  }

  if (y->p == NULL) {
    root = x;
  } else {
    if (y == y->p->l) {
      y->p->l = x;
    } else {
      y->p->r = x;
    }
  }

  if (y != z) {
    z->k = y->k;
  }

  free(y);
}

void preOrder(Node *node) {
  if (node == NULL)
    return;
  printf(" %d", node->k);
  preOrder(node->l);
  preOrder(node->r);
}

void inOrder(Node *node) {
  if (node == NULL)
    return;
  inOrder(node->l);
  printf(" %d", node->k);
  inOrder(node->r);
}

int main() {
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%s", com);
    if (com[0] == 'i') {
      scanf("%d", &k);
      insert(k);
    } else if (com[0] == 'f') {
      scanf("%d", &k);
      if (find(k) != NULL)
        printf("yes\\n");
      else
        printf("no\\n");
    } else if (com[0] == 'd') {
      scanf("%d", &k);
      deleteNode(find(k));
    } else if (com[0] == 'p') {
      inOrder(root);
      printf("\\n");
      preOrder(root);
      printf("\\n");
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
#define _USE_MATH_DEFINES // M_PI????????????????????????
#include <math.h>
#pragma warning(disable : 4996)
using namespace std;

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

Node *root, *NIL;

Node *treeMinimum(Node *x) {
  while (x->left != NIL)
    x = x->left;
  return x;
}

Node *find(Node *u, int k) {
  while (u != NIL && k != u->key) {
    if (k < u->key)
      u = u->left;
    else
      u = u->right;
  }
  return u;
}

Node *treeSuccessor(Node *x) {
  if (x->right != NIL)
    return treeMinimum(x->right);
  Node *y = x->parent;
  while (y != NIL && x == y->right) {
    x = y;
    y = y->parent;
  }
  return y;
}

void treeDelete(Node *z) {
  Node *y; // ????????????????±?
  Node *x; // y??????

  // ??????????????????????±???????
  if (z->left == NIL || z->right == NIL)
    y = z;
  else
    y = treeSuccessor(z);

  // y??????x????±???????
  if (y->left != NIL) {
    x = y->left;
  } else {
    x = y->right;
  }
  if (x != NIL) {
    x->parent = y->parent;
  }
  if (y->parent == NIL) {
    root = x;
  } else {
    if (y == y->parent->left) {
      y->parent->left = x;
    } else {
      y->parent->right = x;
    }
  }

  if (y != z) {
    z->key = y->key;
  }
  free(y);
}

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }

  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    }
  }
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  cout << u->key;
  inorder(u->right);
}

void preorder(Node *u) {
  if (u == NIL)
    return;
  cout << u->key;
  preorder(u->left);
  preorder(u->right);
}

int main() {
  int n, i, x;
  string com;
  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> com;
    if (com[0] == 'f') {
      cin >> x;
      Node *t = find(root, x);
      if (t != NIL)
        cout << "yes\\n";
      else
        cout << "no\\n";
    } else if (com == "insert") {
      cin >> x;
      insert(x);
    } else if (com == "print") {
      inorder(root);
      cout << "\\n";
      preorder(root);
      cout << "\\n";
    } else if (com == "delete") {
      cin >> x;
      treeDelete(find(root, x));
    }
  }
  return 0;
}