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

#include <assert.h>
#include <climits>
#include <iostream>
#include <string>

struct TNode {
  int Value;
  TNode *Parent;
  TNode *Left;
  TNode *Right;

  TNode();
  TNode(int AValue, TNode *AParent);
};

TNode::TNode() {
  Value = INT_MAX;
  Parent = NULL;
  Left = NULL;
  Right = NULL;
}

TNode::TNode(int AValue, TNode *AParent) {
  Value = AValue;
  Parent = AParent;
  Left = NULL;
  Right = NULL;
}
void Insert(int Key, TNode *Root);
void Delete(TNode *Root) {
  if (Root == NULL)
    return;
  Delete(Root->Left);
  Delete(Root->Right);
  delete Root;
}

void AddLeft(int Key, TNode *Root) {
  assert(Root != NULL);
  if (Root->Left == NULL) {
    TNode *Left = new TNode(Key, Root);
    Root->Left = Left;
  } else {
    Insert(Key, Root->Left);
  }
}

void AddRight(int Key, TNode *Root) {
  assert(Root != NULL);
  if (Root->Right == NULL) {
    TNode *Right = new TNode(Key, Root);
    Root->Right = Right;
  } else {
    Insert(Key, Root->Right);
  }
}

void Insert(int Key, TNode *Root) {
  assert(Root != NULL);
  if (Root->Value == Key)
    return;
  if (Root->Value == INT_MAX) {
    Root->Value = Key;
    return;
  }
  if (Root->Value > Key) {
    AddLeft(Key, Root);
  } else {
    AddRight(Key, Root);
  }
}

bool Find(int Key, TNode *Node) {
  if (Node == NULL)
    return false;
  if (Node->Value == Key)
    return true;
  if (Node->Value > Key)
    return Find(Key, Node->Left);
  return Find(Key, Node->Right);
}

void OutputPreOrder(const TNode *Root) {
  if (Root == NULL)
    return;
  std::cout << " " << Root->Value;
  OutputPreOrder(Root->Left);
  OutputPreOrder(Root->Right);
}

void OutputInorder(const TNode *Root) {
  if (Root == NULL)
    return;
  OutputInorder(Root->Left);
  std::cout << " " << Root->Value;
  OutputInorder(Root->Right);
}

void Print(const TNode *Node) {
  OutputInorder(Node);
  std::cout << std::endl;
  OutputPreOrder(Node);
  std::cout << std::endl;
}

void Execute(const std::string &Command, TNode *Root) {
  if (Command == "insert") {
    int Key = 0;
    std::cin >> Key;
    Insert(Key, Root);
    return;
  }
  if (Command == "find") {
    int Key = 0;
    std::cin >> Key;
    if (Find(Key, Root)) {
      std::cout << "yes" << std::endl;
      return;
    }
    std::cout << "no" << std::endl;
  }
  if (Command == "print") {
    Print(Root);
  }
}

int main() {
  int Count = 0;
  std::cin >> Count;
  TNode *Root = new TNode();
  for (int i = 0; i < Count; ++i) {
    std::string Command;
    std::cin >> Command;
    Execute(Command, Root);
  }
  Delete(Root);
  return 0;
}

Error: TLE

Fixed submission:

#include <assert.h>
#include <climits>
#include <iostream>
#include <string>

struct TNode {
  int Value;
  TNode *Parent;
  TNode *Left;
  TNode *Right;

  TNode();
  TNode(int AValue, TNode *AParent);
};

TNode::TNode() {
  Value = INT_MAX;
  Parent = NULL;
  Left = NULL;
  Right = NULL;
}

TNode::TNode(int AValue, TNode *AParent) {
  Value = AValue;
  Parent = AParent;
  Left = NULL;
  Right = NULL;
}
void Insert(int Key, TNode *Root);
void Delete(TNode *Root) {
  if (Root == NULL)
    return;
  Delete(Root->Left);
  Delete(Root->Right);
  delete Root;
}

void AddLeft(int Key, TNode *Root) {
  assert(Root != NULL);
  if (Root->Left == NULL) {
    TNode *Left = new TNode(Key, Root);
    Root->Left = Left;
  } else {
    Insert(Key, Root->Left);
  }
}

void AddRight(int Key, TNode *Root) {
  assert(Root != NULL);
  if (Root->Right == NULL) {
    TNode *Right = new TNode(Key, Root);
    Root->Right = Right;
  } else {
    Insert(Key, Root->Right);
  }
}

void Insert(int Key, TNode *Root) {
  assert(Root != NULL);
  if (Root->Value == Key)
    return;
  if (Root->Value == INT_MAX) {
    Root->Value = Key;
    return;
  }
  if (Root->Value > Key) {
    AddLeft(Key, Root);
  } else {
    AddRight(Key, Root);
  }
}

bool Find(int Key, TNode *Node) {
  if (Node == NULL)
    return false;
  if (Node->Value == Key)
    return true;
  return Find(Key, Node->Left) || Find(Key, Node->Right);
}

void OutputPreOrder(const TNode *Root) {
  if (Root == NULL)
    return;
  std::cout << " " << Root->Value;
  OutputPreOrder(Root->Left);
  OutputPreOrder(Root->Right);
}

void OutputInorder(const TNode *Root) {
  if (Root == NULL)
    return;
  OutputInorder(Root->Left);
  std::cout << " " << Root->Value;
  OutputInorder(Root->Right);
}

void Print(const TNode *Node) {
  OutputInorder(Node);
  std::cout << std::endl;
  OutputPreOrder(Node);
  std::cout << std::endl;
}

void Execute(const std::string &Command, TNode *Root) {
  if (Command == "insert") {
    int Key = 0;
    std::cin >> Key;
    Insert(Key, Root);
    return;
  }
  if (Command == "find") {
    int Key = 0;
    std::cin >> Key;
    if (Find(Key, Root)) {
      std::cout << "yes" << std::endl;
      return;
    }
    std::cout << "no" << std::endl;
  }
  if (Command == "print") {
    Print(Root);
  }
}

int main() {
  int Count = 0;
  std::cin >> Count;
  TNode *Root = new TNode();
  for (int i = 0; i < Count; ++i) {
    std::string Command;
    std::cin >> Command;
    Execute(Command, Root);
  }
  Delete(Root);
  return 0;
}