愚蠢一把
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
private:
vector<int>ans;
public:
BSTIterator(TreeNode* root) {
insert(root);
sort(ans.begin(),ans.end());
}
void insert(TreeNode* root)
{
if(root)
{
insert(root->left);
ans.push_back(root->val);
insert(root->right);
}
}
/** @return the next smallest number */
int next() {
int num = ans[0];
ans.erase(ans.begin());
return num;
}
/** @return whether we have a next smallest number */
bool hasNext() {
return ans.size();
}
};
/**
* 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();
*/