Nice solution. It is actually breaks in-order traversal into hasNext() and next()
class BSTIterator {
public:
stack<TreeNode *> s;
BSTIterator(TreeNode *root)
{
pushAll(root);
}
bool hasNext()
{
return !s.empty();
}
int next()
{
TreeNode *tmp=s.top();
s.pop();
pushAll(tmp->right);
return tem->val;
}
void pushAll(TreeNode*node)
{
while(node)
{
s.push(node);
node=node->left;
}
}
};