-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST iterator.cpp
More file actions
63 lines (51 loc) · 1.22 KB
/
BST iterator.cpp
File metadata and controls
63 lines (51 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
/************************************************************
Following is the TreeNode class structure
template <typename T>
class TreeNode {
public:
T data;
TreeNode<T> *left;
TreeNode<T> *right;
TreeNode(T data) {
this->data = data;
left = NULL;
right = NULL;
}
};
************************************************************/
class BSTiterator
{ public:
int index;
vector<int> store;
BSTiterator(TreeNode<int> *root)
{
index=0;
inorder(root);
}
int next()
{
int next=store[index];
index++;
return next;
}
bool hasNext()
{
if(index<store.size()) return true;
return false;
}
void inorder(TreeNode<int>* node){
if(node==NULL) return ;
inorder(node->left);
store.push_back(node->data);
inorder(node->right);
}
};
/*
Your BSTIterator object will be instantiated and called as such:
BSTIterator iterator(root);
while(iterator.hasNext())
{
print(iterator.next());
}
*/