-
Notifications
You must be signed in to change notification settings - Fork 258
/
search-range-in-binary-search-tree.cpp
93 lines (84 loc) · 2.6 KB
/
search-range-in-binary-search-tree.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
// Time: O(n)
// Space: O(h)
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
// Recursive solution.
class Solution {
public:
/**
* @param root: The root of the binary search tree.
* @param k1 and k2: range k1 to k2.
* @return: Return all keys that k1<=key<=k2 in ascending order.
*/
vector<int> searchRange(TreeNode* root, int k1, int k2) {
vector<int> result;
pair<int, int> interval{k1, k2};
RangeLookupInBSTHelper(root, interval, &result);
return result;
}
void RangeLookupInBSTHelper(const TreeNode* tree,
const pair<int, int>& interval,
vector<int>* result) {
if (tree == nullptr) {
return;
}
if (interval.first <= tree->val && tree->val <= interval.second) {
// tree->data lies in the interval.
RangeLookupInBSTHelper(tree->left, interval, result);
result->emplace_back(tree->val);
RangeLookupInBSTHelper(tree->right, interval, result);
} else if (interval.first > tree->val) {
RangeLookupInBSTHelper(tree->right, interval, result);
} else { // interval.second > tree->val
RangeLookupInBSTHelper(tree->left, interval, result);
}
}
};
// Iterative solution.
class Solution2 {
public:
/**
* @param root: The root of the binary search tree.
* @param k1 and k2: range k1 to k2.
* @return: Return all keys that k1<=key<=k2 in ascending order.
*/
vector<int> searchRange(TreeNode* root, int k1, int k2) {
stack<TreeNode *> st;
vector<int> output;
pushLeft(st, root, k1);
while (st.size() > 0) {
TreeNode *t = st.top();
st.pop();
// Add valid nodes to the stack.
if (t->val >= k1 && t->val <= k2) {
output.emplace_back(t->val);
}
// Push until the min of right descendant.
if (t->val <= k2) {
pushLeft(st, t->right, k1);
}
}
return output;
}
private:
// Add each valid node to the stack until invalid value appears.
void pushLeft(stack<TreeNode *> &st, TreeNode *root, int k1) {
while (root != nullptr) { // Push until invalid.
st.emplace(root);
if (root->val < k1) {
break;
}
root = root->left;
}
}
};