-
Notifications
You must be signed in to change notification settings - Fork 27
/
173. Binary Search Tree Iterator.c
104 lines (89 loc) · 2.59 KB
/
173. Binary Search Tree Iterator.c
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
/*
173. Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Calling next() will return the next smallest number in the BST.
Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
Credits:Special thanks to @ts for adding this problem and creating all test cases.
*/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct BSTIterator {
struct TreeNode **stack;
int sp;
};
int depth(struct TreeNode *node) {
int l, r;
if (!node) return 0;
l = depth(node->left) + 1;
r = depth(node->right) + 1;
if (l > r) return l;
return r;
}
void push_left(struct BSTIterator *obj, struct TreeNode *node) {
do {
obj->stack[obj->sp ++] = node;
node = node->left;
} while (node);
}
struct BSTIterator *bstIteratorCreate(struct TreeNode *root) {
struct BSTIterator *obj;
int d = depth(root);
obj = calloc(1, sizeof(*obj));
//assert(obj);
if (d) {
obj->stack = malloc(d * sizeof(struct TreeNode *));
//assert(obj->stack);
push_left(obj, root);
}
return obj;
}
/** @return whether we have a next smallest number */
bool bstIteratorHasNext(struct BSTIterator *iter) {
return (iter->sp) ? true : false;
}
/** @return the next smallest number */
int bstIteratorNext(struct BSTIterator *iter) {
struct TreeNode *node;
node = iter->stack[-- iter->sp];
if (node->right) push_left(iter, node->right);
return node->val;
}
/** Deallocates memory previously allocated for the iterator */
void bstIteratorFree(struct BSTIterator *iter) {
if (iter->stack) free(iter->stack);
free(iter);
}
/**
* Your BSTIterator will be called like this:
* struct BSTIterator *i = bstIteratorCreate(root);
* while (bstIteratorHasNext(i)) printf("%d\n", bstIteratorNext(i));
* bstIteratorFree(i);
*/
/*
Difficulty:Medium
Total Accepted:95.4K
Total Submissions:231.1K
Companies LinkedIn Google Facebook Microsoft
Related Topics Tree Stack Design
Similar Questions
Binary Tree Inorder Traversal
Flatten 2D Vector
Zigzag Iterator
Peeking Iterator
Inorder Successor in BST
*/