http://www.lintcode.com/en/problem/segment-tree-modify/

解题思路是dfs到Modify的最根的结点(start == end) 然后一路改上来,注意不是这个新值和旧最大值比,因为旧的最大值有可能正好是改变的值,所以还是左右子树的Max比出一个大max,一路到最上的结点。

public void modify(SegmentTreeNode root, int index, int value) {
        // write your code here
        if (root.start == root.end) {
            root.max = value;
            return;
        }

        int mid = (root.start + root.end) / 2;

        if (index > mid) {
            modify(root.right, index, value);
        } else {
            modify(root.left, index, value);
        }

        root.max = Math.max(root.left.max, root.right.max);

    }

results matching ""

    No results matching ""