text: /**
   * Definition for a binary tree node.
   * struct TreeNode {
   *     int val;
   *     TreeNode *left;
   *     TreeNode *right;
   *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
   *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
   *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
   * };
   */
  class BSTIterator {
  public:
      stack<TreeNode *> s;
      BSTIterator(TreeNode* root) {
          while(!s.empty()) {
              s.pop();
          }
          while(root != NULL) {
              s.push(root);
              root = root->left;
          }
      }
      
      /** @return the next smallest number */
      int next() {
          TreeNode *node = s.top(), *temp = node->right;
          s.pop();
          while(temp != NULL) {
              s.push(temp);
              temp = temp->left;
          }
          return node->val;
      }
      
      /** @return whether we have a next smallest number */
      bool hasNext() {
          return !s.empty();
      }
  };
  
  /**
   * Your BSTIterator object will be instantiated and called as such:
   * BSTIterator* obj = new BSTIterator(root);
   * int param_1 = obj->next();
   * bool param_2 = obj->hasNext();
   */
  