Segment Tree Modify(线段树的修改)

http://www.lintcode.com/zh-cn/problem/segment-tree-modify/?rand=true
请先参阅 Segment Tree Query(线段树的查询)

/**
 * Definition of SegmentTreeNode:
 * public class SegmentTreeNode {
 * public int start, end, max;
 * public SegmentTreeNode left, right;
 * public SegmentTreeNode(int start, int end, int max) {
 * this.start = start;
 * this.end = end;
 * this.max = max
 * this.left = this.right = null;
 * }
 * }
 */


public class Solution {
    /*
     * @param root: The root of segment tree.
     * @param index: index.
     * @param value: value
     * @return:
     */
    public void modify(SegmentTreeNode root, int index, int value) {
        // write your code here
        if (root == null || index < root.start || index > root.end) {
            return;
        }
        
        if (index == root.start && index == root.end) {
            root.max = value;
            return;
        }
        int mid = (root.start + root.end) >>> 1;
        
        if (index <= mid) {
            modify(root.left, index, value);
        } else {
            modify(root.right, index, value);
        }
        root.max = Math.max(root.left.max, root.right.max);
    }
}

你可能感兴趣的:(Segment Tree Modify(线段树的修改))