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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
string s;
int x, m, i;
struct node {
  int data;
  struct node *lchild;
  struct node *rchild;
};
typedef struct node;
typedef node *tree;
tree root;
void findn(tree bt, int n) {
  if (bt) {
    if (n < bt->data)
      findn(bt->lchild, n);
    else if (n > bt->data)
      findn(bt->rchild, n);
    else
      cout << "yes" << endl;
  } else
    cout << "no" << endl;
}
void insert(tree &bt, int n) {
  if (bt) {
    if (n < bt->data)
      insert(bt->lchild, n);
    else if (n > bt->data)
      insert(bt->rchild, n);
  } else {
    bt = new node;
    bt->data = n;
    bt->lchild = bt->rchild = NULL;
  }
}

void printh(tree bt) {
  if (bt) {
    if (bt->data != -99)
      cout << " " << bt->data;
    printh(bt->lchild);
    printh(bt->rchild);
  }
}
void printz(tree bt) {
  if (bt) {
    printz(bt->lchild);
    if (bt->data != -99)
      cout << " " << bt->data;
    printz(bt->rchild);
  }
}
void delet(tree &bt, int z) {
  if (bt->data > z)
    delet(bt->lchild, z);
  else if (bt->data < z)
    delet(bt->rchild, z);
  else {
    if (bt->lchild && bt->rchild) {
      tree tmp = bt->rchild;
      while (tmp->lchild) {
        tmp = tmp->lchild;
      }
      bt->data = tmp->data;
      delet(bt->rchild, tmp->data);
    } else {
      tree tmp = bt;
      if (bt->rchild != NULL) {
        bt = bt->rchild;
      } else if (bt->lchild != NULL) {
        bt = bt->lchild;
      } else
        bt->data = -99;
    }
  }
}
int main() {
  root = NULL;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {

    cin >> s;

    if (s == "insert") {
      cin >> x;
      insert(root, x);
    }
    if (s == "find") {
      cin >> x;
      findn(root, x);
    }
    if (s == "print") {
      printz(root);
      cout << endl;
      printh(root);
      cout << endl;
    }
    if (s == "delete") {
      int c;
      scanf("%d", &c);
      delet(root, c);
    }
  }

  // system("pause");
  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

struct node {
  node *r, *l;
  node *f;
  int num;
  node() { num = 0; }
} *root, *NIL;

void insert(int m, node *&T) {
  if (T) {
    if (T->num > m) {
      insert(m, T->l);
      T->l->f = T;
    }
    if (T->num < m) {
      insert(m, T->r);
      T->r->f = T;
    }
  } else {
    T = new node;
    T->num = m;
    T->l = T->r = NIL;
  }
}

void morder(node *T) {
  if (T) {
    morder(T->l);
    cout << " " << T->num;
    morder(T->r);
  } else
    return;
}

void porder(node *T) {
  if (T) {
    cout << " " << T->num;
    porder(T->l);
    porder(T->r);
  } else
    return;
}

node *find(int m, node *T) {
  node *p = T;
  if (p == NIL) {
    return NIL;
  } else if (p->num == m) {
    return p;
  } else if (p->num < m) {
    find(m, p->r);
  } else if (p->num > m) {
    find(m, p->l);
  }
}

void erase(int m, node *T) {
  node *p = T;
  p = find(m, p);
  if (p->r == NIL && p->l == NIL) {
    if (p->f->l == p) {
      p->f->l = NIL;
    }
    if (p->f->r == p) {
      p->f->r = NIL;
    }
    free(p);
  }
  if (p->r == NIL && p->l != NIL) {
    if (p->f) {
      p->l->f = p->f;
      p->f->l = p->l;
      // free(p);
    } else {
      root = p->l;
      p->l->f = NIL;
      // free(p);
    }
  }
  if (p->r != NIL && p->l == NIL) {
    if (p->f) {
      p->r->f = p->f;
      p->f->r = p->r;
      // free(p);
    } else {
      root = p->r;
      p->r->f = NIL;
      // free(p);
    }
  }
  if (p->r != NIL && p->l != NIL) {
    node *temp = p;
    p = p->r;
    while (p->l) {
      p = p->l;
    }
    temp->num = p->num;
    if (p->f == temp) {
      if (p->r) {
        p->r->f = p->f;
        p->f->r = p->r;
      } else {
        p->f->r = NIL;
      }
    } else {
      if (p->r) {
        p->r->f = p->f;
        p->f->l = p->r;
      } else {
        p->f->l = NIL;
      }
    }
    // free(p);
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    string c;
    cin >> c;
    if (c == "insert") {
      int m;
      cin >> m;
      insert(m, root);
      root->f = NIL;
    }
    if (c == "print") {
      morder(root);
      cout << endl;
      porder(root);
      cout << endl;
    }
    if (c == "find") {
      int m;
      cin >> m;
      node *temp = find(m, root);
      if (temp)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
    if (c == "delete") {
      int m;
      cin >> m;
      erase(m, root);
    }
    if (c == "dad") {
      int m;
      cin >> m;
      node *temp = find(m, root);
      if (temp)
        cout << temp->f->num << endl;
    }
    if (c == "l") {
      int m;
      cin >> m;
      node *temp = find(m, root);
      if (temp)
        cout << temp->l->num << endl;
    }
    if (c == "r") {
      int m;
      cin >> m;
      node *temp = find(m, root);
      if (temp)
        cout << temp->r->num << endl;
    }
  }
  return 0;
}