Problem p02284 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

class Node {
public:
  Node() {
    this->_val = 0;
    this->_parent = -1;
    this->_left = -1;
    this->_right = -1;
  }
  Node(int val) {
    this->_val = val;
    this->_parent = -1;
    this->_left = -1;
    this->_right = -1;
  };
  ~Node() { ; };

  const int &val() const { return this->_val; }
  int &val() { return this->_val; }

  const int &parent() const { return this->_parent; }
  int &parent() { return this->_parent; }

  const int &left() const { return this->_left; }
  int &left() { return this->_left; }

  const int &right() const { return this->_right; }
  int &right() { return this->_right; }

private:
  int _val;
  int _parent;
  int _left;
  int _right;
};

std::vector<Node> nodelist(500000);
const int rootid = 0;
int counter = 0;

void insert(int newval) {
  if (counter == 0) {
    nodelist[rootid].val() = newval;
    return;
  }

  int y;          // ?????¨??°????????????????????????
  int x = rootid; // ?????¨??°??????????????????
  while (x != -1) {
    y = x;
    if (newval < nodelist[x].val()) {
      x = nodelist[x].left(); // ?????????????§????
    } else {
      x = nodelist[x].right();
    }
  }
  nodelist[counter].parent() = y;
  nodelist[counter].val() = newval;

  if (newval < nodelist[y].val()) {
    nodelist[y].left() = counter; // ?????????????????????
  } else {
    nodelist[y].right() = counter; // ?????????????????????
  }
}

void find(int val, int curid) {
  if (nodelist[curid].val() == val) {
    cout << "yes" << endl;
    return;
  } else {
    if (nodelist[curid].left() == -1 && nodelist[curid].right() == -1) {
      cout << "no" << endl;
      return;
    } else if (val < nodelist[curid].val()) {
      if (nodelist[curid].left() == -1) {
        cout << "no" << endl;
        return;
      } else {
        find(val, nodelist[curid].left());
      }
    } else {
      if (nodelist[curid].right() == -1) {
        cout << "no" << endl;
        return;
      } else {
        find(val, nodelist[curid].right());
      }
    }
  }
}

void inorder(int id) {
  if (nodelist[id].left() != -1) {
    inorder(nodelist[id].left());
  }
  cout << " " << nodelist[id].val();
  if (nodelist[id].right() != -1) {
    inorder(nodelist[id].right());
  }
}

void preorder(int id) {
  cout << " " << nodelist[id].val();
  if (nodelist[id].left() != -1) {
    preorder(nodelist[id].left());
  }
  if (nodelist[id].right() != -1) {
    preorder(nodelist[id].right());
  }
}

int main() {
  int n;
  cin >> n;
  std::string op;
  int k;
  for (int i = 0; i < n; i++) {
    cin >> op;
    if (op == "insert") {
      cin >> k;
      insert(k);
      counter++;
    } else if (op == "find") {
      cin >> k;
      find(k, rootid);
    } else if (op == "print") {
      // cout << "Inorder:" << endl;
      inorder(rootid);
      cout << endl;
      // cout << "Preorder:" << endl;
      preorder(rootid);
      cout << endl;
    }
  }

  // debug
  // for (int i = 0; i < counter; i++) {
  //     cout << "id = " << i << endl
  //          << "val = " << nodelist[i].val() << endl
  //          << "parent = " << nodelist[i].parent() << endl
  //          << "left = " << nodelist[i].left() << endl
  //          << "right = " << nodelist[i].right() << endl
  //          << endl;
  // }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

class Node {
public:
  Node() {
    this->_val = 0;
    this->_parent = -1;
    this->_left = -1;
    this->_right = -1;
  }
  Node(int val) {
    this->_val = val;
    this->_parent = -1;
    this->_left = -1;
    this->_right = -1;
  };
  ~Node() { ; };

  const int &val() const { return this->_val; }
  int &val() { return this->_val; }

  const int &parent() const { return this->_parent; }
  int &parent() { return this->_parent; }

  const int &left() const { return this->_left; }
  int &left() { return this->_left; }

  const int &right() const { return this->_right; }
  int &right() { return this->_right; }

private:
  int _val;
  int _parent;
  int _left;
  int _right;
};

std::vector<Node> nodelist(500000);
const int rootid = 0;
int counter = 0;

void insert(int newval) {
  if (counter == 0) {
    nodelist[rootid].val() = newval;
    return;
  }

  int y;          // ?????¨??°????????????????????????
  int x = rootid; // ?????¨??°??????????????????
  while (x != -1) {
    y = x;
    if (newval < nodelist[x].val()) {
      x = nodelist[x].left(); // ?????????????§????
    } else {
      x = nodelist[x].right();
    }
  }
  nodelist[counter].parent() = y;
  nodelist[counter].val() = newval;

  if (newval < nodelist[y].val()) {
    nodelist[y].left() = counter; // ?????????????????????
  } else {
    nodelist[y].right() = counter; // ?????????????????????
  }
}

void find(int val, int curid) {
  if (nodelist[curid].val() == val) {
    cout << "yes" << endl;
    return;
  } else {
    if (nodelist[curid].left() == -1 && nodelist[curid].right() == -1) {
      cout << "no" << endl;
      return;
    } else if (val < nodelist[curid].val()) {
      find(val, nodelist[curid].left());
    } else {
      find(val, nodelist[curid].right());
    }
  }
}

void inorder(int id) {
  if (nodelist[id].left() != -1) {
    inorder(nodelist[id].left());
  }
  cout << " " << nodelist[id].val();
  if (nodelist[id].right() != -1) {
    inorder(nodelist[id].right());
  }
}

void preorder(int id) {
  cout << " " << nodelist[id].val();
  if (nodelist[id].left() != -1) {
    preorder(nodelist[id].left());
  }
  if (nodelist[id].right() != -1) {
    preorder(nodelist[id].right());
  }
}

int main() {
  int n;
  cin >> n;
  std::string op;
  int k;
  for (int i = 0; i < n; i++) {
    cin >> op;
    if (op == "insert") {
      cin >> k;
      insert(k);
      counter++;
    } else if (op == "find") {
      cin >> k;
      find(k, rootid);
    } else if (op == "print") {
      // cout << "Inorder:" << endl;
      inorder(rootid);
      cout << endl;
      // cout << "Preorder:" << endl;
      preorder(rootid);
      cout << endl;
    }
  }

  // debug
  // for (int i = 0; i < counter; i++) {
  //     cout << "id = " << i << endl
  //          << "val = " << nodelist[i].val() << endl
  //          << "parent = " << nodelist[i].parent() << endl
  //          << "left = " << nodelist[i].left() << endl
  //          << "right = " << nodelist[i].right() << endl
  //          << endl;
  // }

  return 0;
}