# 二叉搜索树迭代器 实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器:

注意,指针初始化为一个不存在于 BST 中的数字,所以对 next() 的首次调用将返回 BST 中的最小元素。

你可以假设 next() 调用总是有效的,也就是说,当调用 next() 时,BST 的中序遍历中至少存在一个下一个数字。

 

示例:

输入
["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
输出
[null, 3, 7, true, 9, true, 15, true, 20, false]

解释
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
bSTIterator.next();    // 返回 3
bSTIterator.next();    // 返回 7
bSTIterator.hasNext(); // 返回 True
bSTIterator.next();    // 返回 9
bSTIterator.hasNext(); // 返回 True
bSTIterator.next();    // 返回 15
bSTIterator.hasNext(); // 返回 True
bSTIterator.next();    // 返回 20
bSTIterator.hasNext(); // 返回 False

 

提示:

 

进阶:

以下错误的选项是?

## aop ### before ```c #include using namespace std; 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) {} }; ``` ### after ```c /** * 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(); */ ``` ## 答案 ```c class BSTIterator { private: void traverse(TreeNode *root, vector &res) { if (root == nullptr) { return; } traverse(root->right, res); traverse(root->left, res); res.push_back(root->val); } vector inorder(TreeNode *root) { vector res; traverse(root, res); return res; } vector arr; int idx; public: BSTIterator(TreeNode *root) : idx(0), arr(inorder(root)) { } int next() { return arr[idx++]; } bool hasNext() { return idx < arr.size(); } }; ``` ## 选项 ### A ```c class BSTIterator { private: void inorder(TreeNode *root, vector &vec) { if (!root) return; inorder(root->left, vec); vec.emplace_back(root->val); inorder(root->right, vec); } vector inorderTraversal(TreeNode *root) { vector ret; inorder(root, ret); return ret; } vector buf; size_t idx; public: BSTIterator(TreeNode *root) { idx = 0; buf = inorderTraversal(root); } int next() { return buf[idx++]; } bool hasNext() { return idx < buf.size(); } }; ``` ### B ```c class BSTIterator { public: stack treeMin; BSTIterator(TreeNode *root) { TreeNode *t = root; while (t) { treeMin.push(t); t = t->left; } } /** @return the next smallest number */ int next() { TreeNode *tmp = treeMin.top(); int res = tmp->val; treeMin.pop(); tmp = tmp->right; while (tmp) { treeMin.push(tmp); tmp = tmp->left; } return res; } /** @return whether we have a next smallest number */ bool hasNext() { if (treeMin.empty()) return false; else return true; } }; ``` ### C ```c class BSTIterator { public: queue q; BSTIterator(TreeNode *root) { inorder(root, q); } void inorder(TreeNode *root, queue &q) { if (root == NULL) return; inorder(root->left, q); q.push(root->val); inorder(root->right, q); } /** @return the next smallest number */ int next() { int tmp = q.front(); q.pop(); return tmp; } /** @return whether we have a next smallest number */ bool hasNext() { if (q.empty()) return false; else return true; } }; ```