lintcode-线段树查询II-247

来源:互联网 发布:超图软件股吧 编辑:程序博客网 时间:2024/06/05 07:16
/* 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(end<mid+1)            return query(root->left,start,end);        else            return query(root->left,start,mid)+query(root->right,mid+1,end);             }};

0 0