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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

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

node *root, *nil;

void insert(int k) {
  node *x = root;
  node *it = nil;
  node *z;

  z = (node *)malloc(sizeof(node));
  z->key = k;
  z->left = nil;
  z->right = nil;

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

  z->parent = it;
  if (it == nil)
    root = z;
  else {
    if (z->key < it->key)
      it->left = z;
    else
      it->right = z;
  }
}

void inorder(node *u) {
  if (u == nil)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preorder(node *u) {
  if (u == nil)
    return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

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

  for (int i = 0; i < n; i++) {
    cin >> cmd;
    if (cmd == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (cmd == "print") {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
struct node {
  int dad;
  int lch, rch;
  bool L, R;
} point[1500002];
int root, num;
int tot;
void Insert(int deal) {
  if (!root) {
    root = deal;
    return;
  }
  int find = root;
  while (1) {
    if (deal < find) {
      if (!point[find].L) {
        point[find].lch = deal;
        point[deal].dad = find;
        point[find].L = 1;
        return;
      }
      find = point[find].lch;
    } else {
      if (!point[find].R) {
        point[find].rch = deal;
        point[deal].dad = find;
        point[find].R = 1;
        return;
      }
      find = point[find].rch;
    }
  }
}
void Delete(int deal) {
  if (!point[deal].L && !point[deal].R && deal != root) {
    if (point[point[deal].dad].lch == deal) {
      if (root != deal)
        point[point[deal].dad].lch = 0;
      else
        point[point[deal].dad].L = 0;
    } else {
      if (root != deal)
        point[point[deal].dad].rch = 0;
      else
        point[point[deal].dad].R = 0;
    }
  } else if (point[deal].L && point[deal].R) {
    point[deal].L = point[deal].R = 0;
    if (point[point[deal].dad].lch == deal) {
      if (root == deal)
        root = point[deal].lch;
      else
        point[point[deal].dad].lch = point[deal].lch,
        point[point[deal].lch].dad = point[deal].dad;
    } else {
      if (root == deal)
        root = point[deal].lch;
      else
        point[point[deal].dad].rch = point[deal].lch,
        point[point[deal].lch].dad = point[deal].dad;
    }
    Insert(point[deal].rch);
  } else if (point[deal].L) {
    if (point[point[deal].dad].lch == deal) {
      if (root == deal)
        root = point[deal].lch;
      point[point[deal].dad].lch = point[deal].lch,
      point[point[deal].lch].dad = point[deal].dad;
    } else {
      if (root == deal)
        root = point[deal].lch;
      point[point[deal].dad].rch = point[deal].lch,
      point[point[deal].lch].dad = point[deal].dad;
    }
    point[deal].dad = point[deal].L = 0;
  } else {
    if (point[point[deal].dad].rch == deal) {
      if (root == deal)
        root = point[deal].rch;
      point[point[deal].dad].lch = point[deal].rch,
      point[point[deal].rch].dad = point[deal].dad;
    } else {
      if (root == deal)
        root = point[deal].rch;
      point[point[deal].dad].rch = point[deal].rch,
      point[point[deal].rch].dad = point[deal].dad;
    }
    point[deal].dad = point[deal].R = 0;
  }
}
void mid(int x) {
  if (point[x].L)
    mid(point[x].lch);
  printf(" %d", x - 500002);
  if (point[x].R)
    mid(point[x].rch);
}
void last(int x) {
  printf(" %d", x - 500002);
  if (point[x].L)
    last(point[x].lch);
  if (point[x].R)
    last(point[x].rch);
}
void Print() {
  mid(root);
  printf("\\n");
  last(root);
  printf("\\n");
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    string str;
    int deal;
    cin >> str;
    if (str == "print")
      Print();
    else {
      cin >> deal;
      if (str == "insert")
        num++, Insert(deal + 500002);
      else
        Delete(deal + 500002), num--;
    }
  }
}