您的位置:首页 > 其它

lintcode-线段树查询II-247

2015-09-24 22:11 330 查看
/* 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);

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: