Problem p02284 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <memory>
#include <string>

namespace BinarySearchTree {
using namespace std;
constexpr int nullNum = -1;

class Node {
public:
  Node(int k, shared_ptr<Node> p, shared_ptr<Node> l, shared_ptr<Node> r)
      : key(k), parent(p), left(l), right(r) {}

  Node(int k) : key(k), parent(nullptr), left(nullptr), right(nullptr) {}

  Node() : key(nullNum), parent(nullptr), left(nullptr), right(nullptr) {}

  //----Getter----
  int Key() { return key; }

  shared_ptr<Node> Left() { return left; }

  shared_ptr<Node> Right() { return right; }

  //----Setter----
  void SetParent(shared_ptr<Node> p) { parent = p; }

  void SetLeft(shared_ptr<Node> l) { left = l; }

  void SetRight(shared_ptr<Node> r) { right = r; }

private:
  int key;
  shared_ptr<Node> parent;
  shared_ptr<Node> left;
  shared_ptr<Node> right;
};

void Find(shared_ptr<Node> &root, int key) {
  shared_ptr<Node> node(root);
  while (node) {
    if (node->Key() == key) {
      std::cout << "yes\\n";
      return;
    } else if (node->Key() < key) {
      node = node->Right();
    } else {
      node = node->Left();
    }
  }
  std::cout << "no\\n";
}

void Insert(shared_ptr<Node> &root, shared_ptr<Node> &z) {
  if (root->Key() == nullNum) {
    root.reset();
    root = std::move(z);
  } else {
    shared_ptr<Node> y = nullptr;
    shared_ptr<Node> x = root;
    while (x) {
      y = x;
      if (z->Key() < x->Key()) {
        x = x->Left();
      } else {
        x = x->Right();
      }
    }

    z->SetParent(y);

    if (z->Key() < y->Key()) {
      y->SetLeft(z);
    } else {
      y->SetRight(z);
    }
  }
}

void InorderTreeWalk(shared_ptr<Node> root) {
  if (root) {
    InorderTreeWalk(root->Left());
    std::cout << " " << root->Key();
    InorderTreeWalk(root->Right());
  }
}

void PreorderTreeWalk(shared_ptr<Node> root) {
  if (root) {
    std::cout << " " << root->Key();
    PreorderTreeWalk(root->Left());
    PreorderTreeWalk(root->Right());
  }
}

} // namespace BinarySearchTree

int main() {
  std::cin.tie(0);

  int n;
  std::string command;
  std::getline(std::cin, command);
  n = std::stoi(command);

  int key;
  std::shared_ptr<BinarySearchTree::Node> tree =
      std::make_shared<BinarySearchTree::Node>();

  for (int i = 0; i < n; ++i) {
    std::getline(std::cin, command);
    if (command[0] == 'i') {
      key = std::stoi(&command[7]);
      std::shared_ptr<BinarySearchTree::Node> node =
          std::make_shared<BinarySearchTree::Node>(key);
      BinarySearchTree::Insert(tree, node);
    } else if (command[0] == 'f') {
      key = std::stoi(&command[5]);
      BinarySearchTree::Find(tree, key);
    } else {
      BinarySearchTree::InorderTreeWalk(tree);
      std::cout << "\\n";
      BinarySearchTree::PreorderTreeWalk(tree);
      std::cout << std::endl;
    }
  }
}


Error: -6
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi


Fixed submission:

#include <iostream>
#include <memory>
#include <string>

namespace BinarySearchTree {
using namespace std;
constexpr int nullNum = -1;

class Node {
public:
  Node(int k, shared_ptr<Node> p, shared_ptr<Node> l, shared_ptr<Node> r)
      : key(k), parent(p), left(l), right(r) {}

  Node(int k) : key(k), parent(nullptr), left(nullptr), right(nullptr) {}

  Node() : key(nullNum), parent(nullptr), left(nullptr), right(nullptr) {}

  //----Getter----
  int Key() { return key; }

  shared_ptr<Node> Left() { return left; }

  shared_ptr<Node> Right() { return right; }

  //----Setter----
  void SetParent(shared_ptr<Node> p) { parent = p; }

  void SetLeft(shared_ptr<Node> l) { left = l; }

  void SetRight(shared_ptr<Node> r) { right = r; }

private:
  int key;
  shared_ptr<Node> parent;
  shared_ptr<Node> left;
  shared_ptr<Node> right;
};

void Find(shared_ptr<Node> &root, int key) {
  shared_ptr<Node> node(root);
  while (node) {
    if (node->Key() == key) {
      std::cout << "yes\\n";
    } else if (node->Key() > key) {
      node = node->Right();
    } else {
      node = node->Left();
    }
  }
  std::cout << "no\\n";
}

void Insert(shared_ptr<Node> root, shared_ptr<Node> &z) {
  if (root->Key() == nullNum) {
    root.reset();
    root = std::move(z);
  } else {
    shared_ptr<Node> y = nullptr;
    shared_ptr<Node> x = root;
    while (x) {
      y = x;
      if (z->Key() < x->Key()) {
        x = x->Left();
      } else {
        x = x->Right();
      }
    }

    z->SetParent(y);

    if (z->Key() < y->Key()) {
      y->SetLeft(z);
    } else {
      y->SetRight(z);
    }
  }
}

void InorderTreeWalk(shared_ptr<Node> root) {
  if (root) {
    InorderTreeWalk(root->Left());
    std::cout << " " << root->Key();
    InorderTreeWalk(root->Right());
  }
}

void PreorderTreeWalk(shared_ptr<Node> root) {
  if (root) {
    std::cout << " " << root->Key();
    PreorderTreeWalk(root->Left());
    PreorderTreeWalk(root->Right());
  }
}

} // namespace BinarySearchTree

int main() {
  std::cin.tie(0);

  int n;
  std::string command;
  std::getline(std::cin, command);
  n = std::stoi(command);

  int key;
  std::shared_ptr<BinarySearchTree::Node> tree =
      std::make_shared<BinarySearchTree::Node>();

  for (int i = 0; i < n; ++i) {
    std::getline(std::cin, command);
    if (command[0] == 'i') {
      key = std::stoi(&command[7]);
      std::shared_ptr<BinarySearchTree::Node> node =
          std::make_shared<BinarySearchTree::Node>(key);
      BinarySearchTree::Insert(tree, node);
    } else if (command[0] = 'f') {
      key = std::stoi(&command[7]);
      BinarySearchTree::Find(tree, key);
    } else {
      BinarySearchTree::InorderTreeWalk(tree);
      std::cout << "\\n";
      BinarySearchTree::PreorderTreeWalk(tree);
      std::cout << std::endl;
    }
  }
}