Problem p02284 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

struct OwnNode {
  int key;
  OwnNode *parent;
  OwnNode *left;
  OwnNode *right;

  OwnNode(int key) : key(key), parent(nullptr), left(nullptr), right(nullptr) {}
};

void Insert(OwnNode *&top, OwnNode *&z) {
  OwnNode *y = nullptr;
  OwnNode *x = top;

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

  z->parent = y;

  if (y == nullptr) {
    top = z;
  } else if (z->key < y->key) {
    y->left = z;
  } else {
    y->right = z;
  }
}

bool Find(OwnNode *top, int key) {
  if (top == nullptr) {
    return false;
  }
  if (top->key == key) {
    return true;
  } else {
    return Find(top->left, key) || Find(top->right, key);
  }
}

void PrintPre(OwnNode *top) {
  printf(" %d", top->key);
  if (top->left != nullptr) {
    PrintPre(top->left);
  }
  if (top->right != nullptr) {
    PrintPre(top->right);
  }
}

void PrintIn(OwnNode *top) {
  if (top->left != nullptr) {
    PrintIn(top->left);
  }
  printf(" %d", top->key);
  if (top->right != nullptr) {
    PrintIn(top->right);
  }
}

void Print(OwnNode *top) {
  if (top == nullptr) {
    return;
  }
  PrintIn(top);
  printf("\n");
  PrintPre(top);
  printf("\n");
}

int main() {
  OwnNode *T;
  OwnNode *z;
  T = nullptr;

  int m;
  cin >> m;

  for (int i = 0; i < m; i++) {
    char command[10];
    // int key;

    scanf("%s", command);

    // string command;

    // cin >> command;

    if (command[0] == 'i') {
      int key;
      scanf("%d", &key);
      z = new OwnNode(key);
      Insert(T, z);
    } else if (command[0] == 'f') {
      int key;
      cin >> key;
      printf(Find(T, key) ? "yes\n" : "no\n");
    } else {
      Print(T);
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

struct OwnNode {
  int key;
  OwnNode *parent;
  OwnNode *left;
  OwnNode *right;

  OwnNode(int key) : key(key), parent(nullptr), left(nullptr), right(nullptr) {}
};

void Insert(OwnNode *&top, OwnNode *&z) {
  OwnNode *y = nullptr;
  OwnNode *x = top;

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

  z->parent = y;

  if (y == nullptr) {
    top = z;
  } else if (z->key < y->key) {
    y->left = z;
  } else {
    y->right = z;
  }
}

bool Find(OwnNode *top, int key) {
  if (top == nullptr) {
    return false;
  }
  if (top->key == key) {
    return true;
  } else {
    if (top->key < key) {
      if (top->right == nullptr) {
        return false;
      } else {
        return Find(top->right, key);
      }
    } else {
      if (top->left == nullptr) {
        return false;
      } else {
        return Find(top->left, key);
      }
    }
  }
}

void PrintPre(OwnNode *top) {
  printf(" %d", top->key);
  if (top->left != nullptr) {
    PrintPre(top->left);
  }
  if (top->right != nullptr) {
    PrintPre(top->right);
  }
}

void PrintIn(OwnNode *top) {
  if (top->left != nullptr) {
    PrintIn(top->left);
  }
  printf(" %d", top->key);
  if (top->right != nullptr) {
    PrintIn(top->right);
  }
}

void Print(OwnNode *top) {
  if (top == nullptr) {
    return;
  }
  PrintIn(top);
  printf("\n");
  PrintPre(top);
  printf("\n");
}

int main() {
  OwnNode *T;
  OwnNode *z;
  T = nullptr;

  int m;
  cin >> m;

  for (int i = 0; i < m; i++) {
    char command[10];
    // int key;

    scanf("%s", command);

    // string command;

    // cin >> command;

    if (command[0] == 'i') {
      int key;
      scanf("%d", &key);
      z = new OwnNode(key);
      Insert(T, z);
    } else if (command[0] == 'f') {
      int key;
      cin >> key;
      printf(Find(T, key) ? "yes\n" : "no\n");
    } else {
      Print(T);
    }
  }

  return 0;
}