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

#include <cassert>
#include <iostream>
#include <string>
using namespace std;

struct TTree {
private:
  struct TNode {
  public:
    int Id;
    TNode *Parent;
    TNode *LChild;
    TNode *RChild;
    TNode(int Value) {
      Id = Value;
      Parent = NULL;
      LChild = NULL;
      RChild = NULL;
    }
  };
  TNode *Root;
  void Delete(const TNode *Node) {
    if (Node == NULL)
      return;
    Delete(Node->LChild);
    Delete(Node->RChild);
    delete Node;
  }
  void InorderOutput(const TNode *Node) {
    if (Node == NULL)
      return;
    InorderOutput(Node->LChild);
    cout << ' ' << Node->Id;
    InorderOutput(Node->RChild);
  }
  void PreorderOutput(const TNode *Node) {
    if (Node == NULL)
      return;
    cout << ' ' << Node->Id;
    PreorderOutput(Node->LChild);
    PreorderOutput(Node->RChild);
  }
  void Insert(TNode *z) {
    assert(z != NULL);
    TNode *y = NULL;
    TNode *x = Root;
    while (x != NULL) {
      y = x;
      if (z->Id < x->Id) {
        x = x->LChild;
      } else {
        x = x->RChild;
      }
    }
    z->Parent = y;
    if (y == NULL) {
      Root = z;
    } else if (z->Id < y->Id) {
      y->LChild = z;
    } else {
      y->RChild = z;
    }
  }
  void Find(TNode *Node, int Value, bool &IsExist) {
    if (Node == NULL)
      return;
    if (Node->Id == Value) {
      cout << "yes" << endl;
      IsExist = true;
      return;
    } else if (Node->Id > Value) {
      Find(Node->LChild, Value, IsExist);
    } else {
      Find(Node->RChild, Value, IsExist);
    }
  }

public:
  TTree() { Root = NULL; }
  ~TTree() { Delete(Root); }
  void Insert(int Id) { Insert(new TNode(Id)); }
  void Find(int Id) {
    bool IsExist = false;
    Find(Root, Id, IsExist);
    if (IsExist == false)
      cout << "no" << endl;
  }
  void Print() {
    InorderOutput(Root);
    cout << endl;
    PreorderOutput(Root);
    cout << endl;
  }
};

int main() {
  int n;
  cin >> n;
  TTree Tree;
  for (int i = 0; i < n; i++) {
    string Command;
    cin >> Command;
    if (Command == "insert") {
      int Id;
      cin >> Id;
      Tree.Insert(Id);
    } else if (Command == "find") {
      int Id;
      cin >> Id;
      Tree.Find(Id);
    } else {
      Tree.Print();
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cassert>
#include <iostream>
#include <string>
using namespace std;

struct TTree {
private:
  struct TNode {
  public:
    int Id;
    TNode *Parent;
    TNode *LChild;
    TNode *RChild;
    TNode(int Value) {
      Id = Value;
      Parent = NULL;
      LChild = NULL;
      RChild = NULL;
    }
  };
  TNode *Root;
  void Delete(const TNode *Node) {
    if (Node == NULL)
      return;
    Delete(Node->LChild);
    Delete(Node->RChild);
    delete Node;
  }
  void InorderOutput(const TNode *Node) {
    if (Node == NULL)
      return;
    InorderOutput(Node->LChild);
    cout << ' ' << Node->Id;
    InorderOutput(Node->RChild);
  }
  void PreorderOutput(const TNode *Node) {
    if (Node == NULL)
      return;
    cout << ' ' << Node->Id;
    PreorderOutput(Node->LChild);
    PreorderOutput(Node->RChild);
  }
  void Insert(TNode *z) {
    assert(z != NULL);
    TNode *y = NULL;
    TNode *x = Root;
    while (x != NULL) {
      y = x;
      if (z->Id < x->Id) {
        x = x->LChild;
      } else {
        x = x->RChild;
      }
    }
    z->Parent = y;
    if (y == NULL) {
      Root = z;
    } else if (z->Id < y->Id) {
      y->LChild = z;
    } else {
      y->RChild = z;
    }
  }
  void Find(TNode *Node, int Value, bool &IsExist) {
    if (Node == NULL)
      return;
    if (Node->Id == Value) {
      cout << "yes" << endl;
      IsExist = true;
      return;
    }
    Find(Node->LChild, Value, IsExist);
    Find(Node->RChild, Value, IsExist);
  }

public:
  TTree() { Root = NULL; }
  ~TTree() { Delete(Root); }
  void Insert(int Id) { Insert(new TNode(Id)); }
  void Find(int Id) {
    bool IsExist = false;
    Find(Root, Id, IsExist);
    if (IsExist == false)
      cout << "no" << endl;
  }
  void Print() {
    InorderOutput(Root);
    cout << endl;
    PreorderOutput(Root);
    cout << endl;
  }
};

int main() {
  int n;
  cin >> n;
  TTree Tree;
  for (int i = 0; i < n; i++) {
    string Command;
    cin >> Command;
    if (Command == "insert") {
      int Id;
      cin >> Id;
      Tree.Insert(Id);
    } else if (Command == "find") {
      int Id;
      cin >> Id;
      Tree.Find(Id);
    } else {
      Tree.Print();
    }
  }
  return 0;
}