247. Segment Tree Query II

最后更新

二刷
09-Jna-2017

利用线段树进行区间查找,重点还是如何判断每一层的覆盖区间,和覆盖去见与当前NODE值域的关系。

public class Solution {

    public int query(SegmentTreeNode root, int start, int end) {
        if (root == null) return 0;
        if (start > root.end || end < root.start) return 0;
        
        int coveredStart = Math.max(start, root.start);
        int coveredEnd = Math.min(end, root.end);
        if (root.start == coveredStart && root.end == coveredEnd) return root.count;
        
        return query(root.left, coveredStart, coveredEnd) + 
               query(root.right, coveredStart, coveredEnd);
    }
}
posted @ 2017-01-10 10:39  哇呀呀..生气啦~  阅读(107)  评论(0编辑  收藏  举报