https://leetcode.com/problems/binary-search-tree-iterator/
利用非递归 中序遍历的思想
class BSTIterator {
private:
stack<TreeNode*> s;
public:
BSTIterator(TreeNode *root) {
while (!s.empty())
s.pop();
while (root)
{
s.push(root);
root = root->left;
}
}
/** @return whether we have a next smallest number */
bool hasNext() {
return !s.empty();
}
/** @return the next smallest number */
int next() {
TreeNode *temp;
int minvalue;
temp = s.top();
s.pop();
minvalue = temp->val;
temp = temp->right;
while (temp)
{
s.push(temp);
temp = temp->left;
}
return minvalue;
}
};