forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment-tree-build-ii.cpp
55 lines (48 loc) · 1.58 KB
/
segment-tree-build-ii.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
// Time: O(n)
// Space: O(h) = O(logn)
/**
* 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 A: a list of integer
*@return: The root of Segment Tree
*/
SegmentTreeNode * build(vector<int>& A) {
return buildRecu(A, 0, A.size() - 1);
}
SegmentTreeNode *buildRecu(const vector<int>& A,
const int start, const int end) {
if (start > end) {
return nullptr;
}
// The root's start and end is given by build method.
SegmentTreeNode *root = new SegmentTreeNode(start, end, INT_MAX);
// If start equals to end, there will be no children for this node.
if (start == end) {
root->max = A[start];
return root;
}
// Left child: start=A.left, end=(A.left + A.right) / 2.
root->left = buildRecu(A, start, (start + end) / 2);
// Right child: start=(A.left + A.right) / 2 + 1, end=A.right.
root->right = buildRecu(A, (start + end) / 2 + 1, end);
const int left_max = root->left != nullptr ? root->left->max : INT_MAX;
const int right_max = root->right != nullptr ? root->right->max : INT_MAX;
// Update max.
root->max = max(left_max, right_max);
return root;
}
};