数据结构

203. 线段树的修改

2018-01-28  本文已影响16人  6默默Welsh

描述

对于一棵 最大线段树, 每个节点包含一个额外的 max 属性,用于存储该节点所代表区间的最大值。

设计一个 modify 的方法,接受三个参数 rootindexvalue。该方法将 root 为根的线段树中 [start, end] = [index, index] 的节点修改为了新的 value ,并确保在修改后,线段树的每个节点的 max 属性仍然具有正确的值。

注意事项

在做此题前,最好先完成线段树的构造线段树查询这两道题目。

样例

对于线段树:

                      [1, 4, max=3]
                    /                \
        [1, 2, max=2]                [3, 4, max=3]
       /              \             /             \
[1, 1, max=2], [2, 2, max=1], [3, 3, max=0], [4, 4, max=3]

如果调用 modify(root, 2, 4), 返回:

                      [1, 4, max=4]
                    /                \
        [1, 2, max=4]                [3, 4, max=3]
       /              \             /             \
[1, 1, max=2], [2, 2, max=4], [3, 3, max=0], [4, 4, max=3]

调用 modify(root, 4, 0), 返回:

                      [1, 4, max=2]
                    /                \
        [1, 2, max=2]                [3, 4, max=0]
       /              \             /             \
[1, 1, max=2], [2, 2, max=1], [3, 3, max=0], [4, 4, max=0]

挑战

时间复杂度 O(h) , h 是线段树的高度

代码

/**
 * 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) {
        if (root.start == root.end && root.start == index) {
            root.max = value;
            return;
        }
        
        int mid = root.start + (root.end - root.start) / 2;
        if (index <= mid) {
            modify(root.left, index, value);
        } else {
            modify(root.right, index, value);
        }
        // 注意此处,不是 root.max = Math.max(root.max, root.left.max)
        /* 比如线段树
         *  "[1,4,max=3]
         *   [1,2,max=2][3,4,max=3]
         *   [1,1,max=2][2,2,max=1][3,3,max=0][4,4,max=3]"
         *   将 [4,4] 位置的值换成 0,如果向上面那么写,值仍是 3
         */
        root.max = Math.max(root.right.max, root.left.max);
        
        return;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读