lintcode-线段树的查询-202

/**
 * 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:
   
    int query(SegmentTreeNode *root,int start,int end){
        if(!root)
            return INT_MIN;
        if(start<=root->start&&end>=root->end)
            return root->max;
        int mid=(root->start+root->end)/2;
        if(midright,start,end);
        else if(endleft,start,end);
        else{
            return max(query(root->left,start,mid),query(root->right,mid+1,end));
        }
    } 

};

你可能感兴趣的:(Lintcode)