lintcode-线段树查询II-247

/* class SegmentTreeNode {
 * public:
 *     int start, end, count;
 *     SegmentTreeNode *left, *right;
 *     SegmentTreeNode(int start, int end, int count) {
 *         this->start = start;
 *         this->end = end;
 *         this->count = count;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
public:
   
    int query(SegmentTreeNode *root, int start, int end) {
        
        if(!root||start>end)
            return 0;
        if(start<=root->start&&end>=root->end)
            return root->count;
        int mid=root->start+(root->end-root->start)/2;
        
        if(start>mid)
            return query(root->right,start,end);
        else if(endleft,start,end);
        else
            return query(root->left,start,mid)+query(root->right,mid+1,end);
         
    }
};

你可能感兴趣的:(Lintcode)