Problem p02284 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

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

  BINNODE(int k) {
    key = k;
    parent = nullptr;
    left = nullptr;
    right = nullptr;
  }
};

void preorderwalk(BINNODE *root) {
  if (root == nullptr)
    return;
  printf(" %d", root->key);
  preorderwalk(root->left);
  preorderwalk(root->right);
  return;
}

void inorderwalk(BINNODE *root) {
  if (root == nullptr)
    return;
  inorderwalk(root->left);
  printf(" %d", root->key);
  inorderwalk(root->right);
  return;
}

bool findnode(int v, BINNODE *root) {
  if (root == nullptr)
    return false;
  if (root->key == v)
    return true;
  return findnode(v, root->left) || findnode(v, root->right);
}

int main() {
  int m, v;
  int i;
  string cmd;
  BINNODE *root = nullptr;
  BINNODE *x;
  BINNODE *y;
  BINNODE *z;
  cin >> m;
  for (i = 0; i < m; ++i) {
    cin >> cmd;
    if (cmd == "find") {
      cin >> v;
      printf(findnode(v, root) ? "yes\n" : "no\n");
    } else if (cmd == "insert") {
      cin >> v;
      z = new BINNODE(v);
      y = nullptr;
      x = root;
      while (x != nullptr) {
        y = x;
        if (z->key < x->key) {
          x = x->left;
        } else {
          x = x->right;
        }
      }
      z->parent = y;

      if (y == nullptr) {
        root = z;
      } else if (z->key < y->key) {
        y->left = z;
      } else {
        y->right = z;
      }
    } else {
      inorderwalk(root);
      printf("\n");
      preorderwalk(root);
      printf("\n");
    }
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

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

  BINNODE(int k) {
    key = k;
    parent = nullptr;
    left = nullptr;
    right = nullptr;
  }
};

void preorderwalk(BINNODE *root) {
  if (root == nullptr)
    return;
  printf(" %d", root->key);
  preorderwalk(root->left);
  preorderwalk(root->right);
  return;
}

void inorderwalk(BINNODE *root) {
  if (root == nullptr)
    return;
  inorderwalk(root->left);
  printf(" %d", root->key);
  inorderwalk(root->right);
  return;
}

bool findnode(int v, BINNODE *root) {
  if (root == nullptr)
    return false;
  if (root->key == v)
    return true;
  return (v < root->key) ? findnode(v, root->left) : findnode(v, root->right);
}

int main() {
  int m, v;
  int i;
  string cmd;
  BINNODE *root = nullptr;
  BINNODE *x;
  BINNODE *y;
  BINNODE *z;
  cin >> m;
  for (i = 0; i < m; ++i) {
    cin >> cmd;
    if (cmd == "find") {
      cin >> v;
      printf(findnode(v, root) ? "yes\n" : "no\n");
    } else if (cmd == "insert") {
      cin >> v;
      z = new BINNODE(v);
      y = nullptr;
      x = root;
      while (x != nullptr) {
        y = x;
        if (z->key < x->key) {
          x = x->left;
        } else {
          x = x->right;
        }
      }
      z->parent = y;

      if (y == nullptr) {
        root = z;
      } else if (z->key < y->key) {
        y->left = z;
      } else {
        y->right = z;
      }
    } else {
      inorderwalk(root);
      printf("\n");
      preorderwalk(root);
      printf("\n");
    }
  }
  return 0;
}