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

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

using namespace std;

int count, minimum;

void pre(vector<vector<int>> &tree, int now) {
  cout << " " << tree[now][0];
  for (int i = 1; i < 3; i++) {
    if (tree[now][i] != minimum) {
      pre(tree, tree[now][i]);
    }
  }
}

void ino(vector<vector<int>> &tree, int now) {
  int size = tree[now].size();
  if (tree[now][1] == minimum && tree[now][2] == minimum) {
    cout << " " << tree[now][0];
  } else {
    if (tree[now][1] != minimum) {
      ino(tree, tree[now][1]);
    }
    cout << " " << tree[now][0];
    if (tree[now][2] != minimum) {
      ino(tree, tree[now][2]);
    }
  }
}

void print(vector<vector<int>> &tree) {
  ino(tree, 0);
  cout << endl;
  pre(tree, 0);
  cout << endl;
}

void insert(vector<vector<int>> &tree, int key, int now) {
  if (count == 0) // root none
  {
    tree[0][0] = key;
  } else {
    if (tree[now][0] > key) // left
    {
      if (tree[now][1] == minimum) // child none
      {
        tree[now][1] = count;
        tree[count][0] = key;
      } else // child exist
      {
        insert(tree, key, tree[now][1]);
      }
    } else // right
    {
      if (tree[now][2] == minimum) // child none
      {
        tree[now][2] = count;
        tree[count][0] = key;
      } else {
        insert(tree, key, tree[now][2]);
      }
    }
  }
}

void find(vector<vector<int>> &tree, int key, int now) {
  if (tree[now][0] == key) {
    cout << "yes" << endl;
    return;
  }
  if (tree[now][0] > key) {
    if (tree[now][1] == minimum) {
      cout << "no" << endl;
    } else {
      find(tree, key, tree[now][1]);
    }
  } else {
    if (tree[now][2] == minimum) {
      cout << "no" << endl;
    } else {
      find(tree, key, tree[now][2]);
    }
  }
}

int main(void) {
  count = 0;
  minimum = numeric_limits<int>::min();
  vector<vector<int>> tree;
  int n, num;
  string command;
  cin >> n;
  tree = vector<vector<int>>(n, vector<int>(3, minimum));

  for (int i = 0; i < n; i++) {
    cin >> command;
    if (command == "insert") {
      cin >> num; // input node num
      insert(tree, num, 0);
      count++;
    } else if (command == "print") {
      print(tree);
    } else if (command == "find") {
      cin >> num;
      find(tree, num, 0);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int count, minimum;

void pre(vector<vector<int>> &tree, int now) {
  cout << " " << tree[now][0];
  for (int i = 1; i < 3; i++) {
    if (tree[now][i] != minimum) {
      pre(tree, tree[now][i]);
    }
  }
}

void ino(vector<vector<int>> &tree, int now) {
  int size = tree[now].size();
  if (tree[now][1] == minimum && tree[now][2] == minimum) {
    cout << " " << tree[now][0];
  } else {
    if (tree[now][1] != minimum) {
      ino(tree, tree[now][1]);
    }
    cout << " " << tree[now][0];
    if (tree[now][2] != minimum) {
      ino(tree, tree[now][2]);
    }
  }
}

void print(vector<vector<int>> &tree) {
  ino(tree, 0);
  cout << endl;
  pre(tree, 0);
  cout << endl;
}

void insert(vector<vector<int>> &tree, int key, int now) {
  if (count == 0) // root none
  {
    tree[0][0] = key;
  } else {
    if (tree[now][0] > key) // left
    {
      if (tree[now][1] == minimum) // child none
      {
        tree[now][1] = count;
        tree[count][0] = key;
      } else // child exist
      {
        insert(tree, key, tree[now][1]);
      }
    } else // right
    {
      if (tree[now][2] == minimum) // child none
      {
        tree[now][2] = count;
        tree[count][0] = key;
      } else {
        insert(tree, key, tree[now][2]);
      }
    }
  }
}

void find(vector<vector<int>> &tree, int key) {
  for (int i = 0; i < count; i++) {
    if (tree[i][0] == key) {
      cout << "yes" << endl;
      return;
    }
  }
  cout << "no" << endl;
}

int main(void) {
  count = 0;
  minimum = numeric_limits<int>::min();
  vector<vector<int>> tree;
  int n, num;
  string command;
  cin >> n;
  tree = vector<vector<int>>(n, vector<int>(3, minimum));

  for (int i = 0; i < n; i++) {
    cin >> command;
    if (command == "insert") {
      cin >> num; // input node num
      insert(tree, num, 0);
      count++;
    } else if (command == "print") {
      print(tree);
    } else if (command == "find") {
      cin >> num;
      find(tree, num);
    }
  }

  return 0;
}