Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BSTIterator {
- stack<TreeNode*>st;
- public:
- BSTIterator(TreeNode* root) {
- mypush(root);
- }
- /** @return whether we have a next smallest number */
- bool hasNext() {
- return !st.empty();
- }
- /** @return the next smallest number */
- int next() {
- TreeNode *temp = st.top();
- st.pop();
- mypush(temp->right);
- return temp->val;
- }
- void mypush(TreeNode* node)
- {
- while(node!=NULL)
- {
- st.push(node);
- node=node->left;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement