aboutsummaryrefslogtreecommitdiff
path: root/BST_iterator.cpp
blob: e50c74accc9aa9785d76d6f19a20e41685fbe2bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "leetcode.h"

class BSTIterator {
public:
  stack<TreeNode *> s;
  BSTIterator(TreeNode *root) { partialOrder(root); }
  int next() {
    TreeNode *top = s.top();
    s.pop();
    partialOrder(top->right);
    return top->val;
  }
  bool hasNext() { return !s.empty(); }
  void partialOrder(TreeNode *root) {
    while (root != NULL) {
      s.push(root);
      root = root->left;
    }
  }
};

int main() {}