-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment Tree Query.cpp
37 lines (35 loc) · 1.13 KB
/
Segment Tree Query.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
/**
* Definition of SegmentTreeNode:
* class SegmentTreeNode {
* public:
* int start, end, max;
* SegmentTreeNode *left, *right;
* SegmentTreeNode(int start, int end, int max) {
* this->start = start;
* this->end = end;
* this->max = max;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
*@param root, start, end: The root of segment tree and
* an segment / interval
*@return: The maximum number in the interval [start, end]
*/
int query(SegmentTreeNode *root, int start, int end) {
// write your code here
if(root->start == root->end) return root->max;
if(root->start == start && root->end == end) return root->max;
if(end <= root->left->end) {
return query(root->left, start, end);
} else if(start >= root->right->start) {
return query(root->right, start, end);
} else {
return max(query(root->left, start, root->left->end),
query(root->right, root->right->start, end));
}
}
};