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

#define scanf_s scanf
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define MAX 500000
#define MAX_ 2000000000
struct tree {
  long long int key = MAX_ + 1;
  tree *left = NULL, *right = NULL, *p = NULL;
};
tree *flag = NULL;
void insert(tree &T, tree &z) {
  tree *y = NULL, *x = &T;
  while (x->key != MAX_ + 1) {
    y = x;
    if (z.key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
    if (x == NULL)
      break;
  }
  z.p = y;
  if (y == NULL) {
    T = z;
  } else if (z.key < y->key) {
    y->left = &z;
  } else {
    y->right = &z;
  }
}
void print_1(tree *T) {
  if (T != NULL) {
    printf(" %lld", T->key);
    print_1(T->left);
    print_1(T->right);
  }
}
void print_2(tree *T) {
  if (T != NULL) {
    print_2(T->left);
    printf(" %lld", T->key);
    print_2(T->right);
  }
}
int find_(tree *T, long long int n) {
  if (T == NULL) {
    return 0;
  }
  if (T->key == n) {
    return 1;
  }
  if (T->key < n) {
    return (find_(T->right, n));
  } else {
    return (find_(T->left, n));
  }
}
void delete_(tree *T, long long int n) {
  if (T != NULL) {
    delete_(T->left, n);
    if (T->key == n || flag != NULL) {

      if (flag != NULL) {
        flag->key = T->key;
        flag = NULL;
      }
      if (T->left == NULL && T->right == NULL) {
        if (T->p->key > T->key) {
          T->p->left = NULL;
        } else {
          T->p->right = NULL;
        }
        return;
      } else if (T->left != NULL && T->right != NULL) {
        flag = T;
      } else {
        if (T->p->left == T) {
          if (T->left != NULL) {
            T->p->left = T->left;
            T->left->p = T->p;
          } else {
            T->p->left = T->right;
            T->right->p = T->p;
          }
        } else {
          if (T->left != NULL) {
            T->p->right = T->left;
            T->left->p = T->p;
          } else {
            T->p->right = T->right;
            T->right->p = T->p;
          }
        }
        return;
      }
    }
    delete_(T->right, n);
  }
}

int main() {
  int m, cou = 0;
  long long int k;
  string s;
  tree t, id[MAX];
  scanf_s("%d", &m);
  for (int i = 0; i < m; ++i) {
    cin >> s;
    if (s == "insert") {
      scanf_s("%lld", &id[cou].key);
      insert(t, id[cou]);
      ++cou;
    } else if (s == "print") {
      print_2(&t);
      printf("\\n");
      print_1(&t);
      printf("\\n");
    } else if (s == "find") {
      scanf_s("%lld", &k);
      if (find_(&t, k))
        printf("yes\\n");
      else
        printf("no\\n");
    } else {
      scanf_s("%lld", &k);
      flag = NULL;
      delete_(&t, k);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#define scanf_s scanf
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define MAX 500 // 000
#define MAX_ 2000000000
struct tree {
  long long int key = MAX_ + 1;
  tree *left = NULL, *right = NULL, *p = NULL;
};
void insert(tree &T, tree &z) {
  tree *y = NULL, *x = &T;
  while (x->key != MAX_ + 1) {
    y = x;
    if (z.key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
    if (x == NULL)
      break;
  }
  z.p = y;
  if (y == NULL) {
    T = z;
  } else if (z.key < y->key) {
    y->left = &z;
  } else {
    y->right = &z;
  }
}
void print_1(tree *T) {
  if (T != NULL) {
    printf(" %lld", T->key);
    print_1(T->left);
    print_1(T->right);
  }
}
void print_2(tree *T) {
  if (T != NULL) {
    print_2(T->left);
    printf(" %lld", T->key);
    print_2(T->right);
  }
}
int find_(tree *T, long long int n) {
  if (T == NULL) {
    return 0;
  }
  if (T->key == n) {
    return 1;
  }
  if (T->key < n) {
    return (find_(T->right, n));
  } else {
    return (find_(T->left, n));
  }
}
void delete_(tree *T, long long int n, tree *flag) {
  if (T != NULL) {
    delete_(T->left, n, flag);
    if (T->key == n || flag != NULL) {
      if (flag != NULL) {
        flag = T;
      }
      if (T->left == NULL && T->right == NULL) {
        if (T->p->left == T) {
          T->p->left = NULL;
        } else {
          T->p->right = NULL;
        }
        return;
      } else if (T->left == NULL || T->right == NULL) {
        if (T->p->left == T) {
          if (T->left != NULL) {
            T->p->left = T->left;
            T->left->p = T->p;
          } else {
            T->p->left = T->right;
            T->right->p = T->p;
          }
        } else {
          if (T->left != NULL) {
            T->p->right = T->right;
            T->right->p = T->p;
          } else {
            T->p->right = T->left;
            T->left->p = T->p;
          }
        }
        return;
      } else {
        flag = T;
      }
    }
    delete_(T->right, n, flag);
  }
}
int main() {
  int m, cou = 0;
  long long int k;
  string s;
  tree t, id[MAX];
  scanf_s("%d", &m);
  for (int i = 0; i < m; ++i) {
    cin >> s;
    if (s == "insert") {
      scanf_s("%lld", &id[cou].key);
      insert(t, id[cou]);
      ++cou;
    } else if (s == "print") {
      print_2(&t);
      printf("\\n");
      print_1(&t);
      printf("\\n");
    } else if (s == "find") {
      scanf_s("%lld", &k);
      if (find_(&t, k))
        printf("yes\\n");
      else
        printf("no\\n");
    } else {
      scanf_s("%lld", &k);
      delete_(&t, k, NULL);
    }
  }
  return 0;
}