fork download
  1. #include <iostream>
  2.  
  3. class Node {
  4. private:
  5. int data;
  6. Node* nextLeftNode;
  7. Node* nextRightNode;
  8.  
  9. public:
  10. Node() : data(NULL), nextLeftNode(nullptr), nextRightNode(nullptr) {};
  11. Node(const int& data) : data(data), nextLeftNode(nullptr), nextRightNode(nullptr) {};
  12.  
  13. void insertNode(Node& newNode) {
  14. if (this->data == NULL) {
  15. this->data = newNode.data;
  16. return;
  17. }
  18.  
  19. if (newNode.data <= data) {
  20. if (this->nextLeftNode == nullptr) {
  21. this->nextLeftNode = &newNode;
  22. return;
  23. }
  24. this->nextLeftNode->insertNode(newNode);
  25. }
  26. else {
  27. if (this->nextRightNode == nullptr) {
  28. this->nextRightNode = &newNode;
  29. return;
  30. }
  31. this->nextRightNode->insertNode(newNode);
  32. }
  33. }
  34.  
  35. int findMinVal() {
  36. Node* tempNode = this;
  37. while (true) {
  38. if (nextLeftNode == NULL) {
  39. break;
  40. }
  41. else {
  42. tempNode = this->nextLeftNode;
  43. }
  44. }
  45. return this->data;
  46. }
  47. };
  48.  
  49. int main() {
  50. int testCase, numberOfInput, val;
  51. std::cin >> testCase;
  52. for (int i = 0; i < testCase; ++i) {
  53. std::cin >> numberOfInput;
  54. Node node;
  55. for (int j = 0; j < numberOfInput; ++j) {
  56. std::cin >> val;
  57. Node newNode(val);
  58. node.insertNode(newNode);
  59. }
  60. std::cout << node.findMinVal() << '\n';
  61. }
  62. }
Time limit exceeded #stdin #stdout 5s 15232KB
stdin
Standard input is empty
stdout
Standard output is empty