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

#include <cstdlib>
#include <iostream>
using namespace std;

struct tree {
  tree *left;
  tree *right;
  tree *parent;
  int val;
};

void printPreorder(tree *root) {
  if (root == NULL)
    return;
  cout << " " << root->val;
  printPreorder(root->left);
  printPreorder(root->right);
  return;
}
void printInorder(tree *root) {
  if (root == NULL)
    return;
  printInorder(root->left);
  cout << " " << root->val;
  printInorder(root->right);
  return;
}

void find(int val, tree *root) {
  if (root->val == val)
    cout << "yes" << endl;
  else {
    tree *next;
    if (val < root->val)
      next = root->left;
    else
      next = root->right;

    if (next)
      find(val, next);
    else
      cout << "no" << endl;
  }
}

void insert(int val, tree *root) {
  tree *next;
  bool flag = val < root->val;
  if (flag)
    next = root->left;
  else
    next = root->right;

  if (next == NULL) {
    tree *newNode = (tree *)malloc(sizeof(tree));
    newNode->val = val;
    newNode->left = NULL;
    newNode->right = NULL;
    newNode->parent = root;
    if (flag)
      root->left = newNode;
    else
      root->right = newNode;
  } else {
    insert(val, next);
  }
}

int main() {
  ios_base::sync_with_stdio(false);

  tree *root = (tree *)malloc(sizeof(tree));

  char order[10];
  int val;
  int numOfOrder;

  cin >> numOfOrder;
  cin >> order;
  cin >> val;

  root->val = val;
  root->left = NULL;
  root->right = NULL;
  root->parent = NULL;

  for (int i = 0; i < numOfOrder - 1; i++) {
    cin >> order;
    if (order[0] == 'i') {
      cin >> val;
      insert(val, root);
    } else if (order[0] == 'p') {
      printInorder(root);
      cout << endl;
      printPreorder(root);
      cout << endl;
    } else {
      cin >> val;
      find(val, root);
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
using namespace std;

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

Node *NIL = NULL;

void preorder(Node *v) {
  if (v == NIL)
    return;
  printf(" %d", v->key);
  preorder(v->left);
  preorder(v->right);
}

void inorder(Node *v) {
  if (v == NIL)
    return;
  inorder(v->left);
  printf(" %d", v->key);
  inorder(v->right);
}

void postorder(Node *v) {
  if (v == NIL)
    return;
  postorder(v->left);
  postorder(v->right);
  printf(" %d", v->key);
}

void insert(Node *&T, Node *z) {
  Node *y = NIL;
  Node *x = T;
  while (x != NIL) {
    y = x;
    if (z->key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z->parent = y;

  if (y == NIL)
    T = z;
  else if (z->key < y->key)
    y->left = z;
  else
    y->right = z;
}

void find(Node *&T, int key) {
  bool flag = false;
  Node *x = T;
  while (x != NIL) {
    if (x->key == key) {
      flag = true;
      break;

    } else {
      if (x->key < key) {
        x = x->right;
      }
      if (x->key > key) {
        x = x->left;
      }
    }
  }

  if (flag) {
    printf("yes\\n");
  } else {
    printf("no\\n");
  }
}

int main() {
  int n;
  scanf("%d", &n);

  Node *T = NIL;
  for (int i = 0; i < n; ++i) {
    char cmd[20];
    int v;
    scanf("%s %d", cmd, &v);
    if (cmd[0] == 'i') {
      Node *p = new Node;
      p->key = v;
      insert(T, p);
    } else if (cmd[0] == 'p') {
      inorder(T);
      printf("\\n");
      preorder(T);
      printf("\\n");
    } else if (cmd[0] == 'f') {
      find(T, v);
    }
  }

  return 0;
}