BSTIterator(TreeNode* root) {
pushAllLeft(root);
}
int next() {
TreeNode* node = stk.top();
stk.pop();
if (node->right) {
pushAllLeft(node->right);
}
return node->val;
}
bool hasNext() {
return !stk.empty();
}
stack<TreeNode*> stk;
void pushAllLeft(TreeNode* node) {
while (node) {
stk.push(node);
node = node->left;
}
}