-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_tree_iterator.cpp
110 lines (94 loc) · 2.68 KB
/
binary_search_tree_iterator.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// https://leetcode.com/problems/binary-search-tree-iterator/
// June 07, 2016
#include <iostream>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x),left(NULL),right(NULL){}
};
class BSTIterator {
public:
BSTIterator(TreeNode *root)
{
head_ = NULL;
if (root)
{
// Keep going left of tree
// Also populate the linked list so that root is the tail of the linked list i.e. bottom most node is head and
// top most node i.e. root is tail
TreeNode* node = root;
while (node)
{
head_ = new LinkedListNode(node,head_);
node = node->left;
}
}
}
/** @return whether we have a next smallest number */
bool hasNext()
{
return (head_ != NULL);
}
/** @return the next smallest number */
int next()
{
int nextVal = head_->treeNode->val;
// a) head_ should move to next of current head_
// b) Also if current head_ have a right subtree then we should traverse left side and push the nodes in the linked list
TreeNode* node = head_->treeNode->right;
head_ = head_->next;
while (node)
{
head_ = new LinkedListNode(node,head_);
node = node->left;
}
return nextVal;
}
private:
struct LinkedListNode
{
TreeNode* treeNode;
LinkedListNode* next;
LinkedListNode(TreeNode* node, LinkedListNode* nextNode=NULL): treeNode(node), next(nextNode) {}
};
private:
LinkedListNode* head_;
};
/**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/
TreeNode* create_binary_search_tree()
{
TreeNode* node0 = new TreeNode(2);
TreeNode* node1 = new TreeNode(15);
TreeNode* node2 = new TreeNode(5);
TreeNode* node5 = new TreeNode(20);
TreeNode* node3 = new TreeNode(10);
TreeNode* node4 = new TreeNode(8);
node0->right = node1;
node1->left = node2;
node1->right = node5;
node2->right = node3;
node3->left = node4;
return node1;
}
int main(int argc, char* argv[])
{
TreeNode* root = create_binary_search_tree();
//TreeNode* root = NULL;
BSTIterator i = BSTIterator(root);
while (i.hasNext()) cout << i.next() << ",";
return 0;
}
/*
stack should have been used to make the code compact.
Though haven't used the funda mentioned in the following link:
http://stackoverflow.com/questions/8550711/struct-in-class
Have a look at this too:
http://en.cppreference.com/w/cpp/language/nested_types
*/