资讯专栏INFORMATION COLUMN

[LintCode] Segment Tree Modify

CoffeX / 2373人阅读

摘要:和其它题目一样,依然是递归的做法。注意若树的结点范围为,那么至少有个数在左子树上,所以语句里用了号。另外,最后一句是调用递归之后的结果,必须写在最后面。

Problem

For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in this node"s interval.

Implement a modify function with three parameter root, index and value to change the node"s value with [start, end] = [index, index] to the new given value. Make sure after this change, every node in segment tree still has the max attribute with the correct value.

Example

For segment tree:

                      [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]

if call modify(root, 2, 4), we can get:

                      [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]

or call modify(root, 4, 0), we can get:

                      [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]
Challenge

Do it in O(h) time, h is the height of the segment tree.

Note

和segment tree其它题目一样,依然是递归的做法。注意:若树的结点范围为0~n,那么至少有n/2个数在左子树上,所以if语句里用了<=号。另外,最后一句root.max = Math.max(root.left.max, root.right.max)是调用递归之后的结果,必须写在最后面。

Solution
public class Solution {
    public void modify(SegmentTreeNode root, int index, int value) {
        if (index > root.end || index < root.start) return;
        if (root.start == root.end) {
            root.max = value;
            return;
        }
        if (index <= (root.start + root.end) / 2) modify(root.left, index, value);
        else modify(root.right, index, value);
        root.max = Math.max(root.left.max, root.right.max);
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66194.html

相关文章

  • [LintCode] Segment Tree Build & Segment Tree B

    摘要:唯一需要注意的就是的赋值取左右子树的的较大值,最后一层的独立结点的为对应数组中的值。 Segment Tree Build I Problem The structure of Segment Tree is a binary tree which each node has two attributes start and end denote an segment / interv...

    honhon 评论0 收藏0
  • [LintCode] Segment Tree Query I & Segment Tree

    摘要:题目是要查询到这个区间内某一点的。值是从最底层的子节点值里取最大值。因此,不用太复杂,递归就可以了。与所不同的是,对所给区间内的元素个数求和,而非筛选。这样就会出现的情况,视作本身处理。 Segment Tree Query Problem For an integer array (index from 0 to n-1, where n is the size of this ar...

    vibiu 评论0 收藏0
  • [LintCode] Interval Minimum Number

    摘要:这道题目是筛选出数组中的最小值,存入新数组。因此,联想到和系列的题目,对于的处理,使用线段树是非常有效的方法。之前我们创建的线段树,有和两个。参照这个参数,可以考虑在这道题增加一个的参数,代表每个结点的最小值。 Problem Given an integer array (index from 0 to n-1, where n is the size of this array),...

    taowen 评论0 收藏0
  • [LintCode] Simplify Path [字符串操作]

    摘要:,可以用函数去掉所有,然后多考虑一个中间为空的。关于语句的一个特点我们对和其实都是不做操作,然而,两个可以都,但是不能都不做操作。像这样这样这两个就都会等价于下一个就会出错。 Problem Given an absolute path for a file (Unix-style), simplify it. Example /home/, => /home //去掉末尾的slash...

    leanxi 评论0 收藏0
  • LintCode: Max Tree

    摘要:题目此题和很类似,所以第一反应使用递归做。递归的解法过不了,会显示超时比递归的更好的方法是用,比较难想到,代码参考了思路是用一个单调递减栈寻找最大值。这个操作可以帮我们顺利找到左子树和父节点。 题目 Given an integer array with no duplicates. A max tree building on this array is defined as fol...

    ivan_qhz 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<