LintCode:线段树的修改

116 篇文章 0 订阅

LintCode:线段树的修改

先更新对应节点的值,再遍历一次修改max值。

"""
Definition of SegmentTreeNode:
class SegmentTreeNode:
    def __init__(self, start, end, max):
        self.start, self.end, self.max = start, end, max
        self.left, self.right = None, None
"""

class Solution: 
    """
    @param root, index, value: The root of segment tree and 
    @ change the node's value with [index, index] to the new given value
    @return: nothing
    """
    def modify(self, root, index, value):
        # write your code here
        self.modify_1(root, index, value)
        self.update(root, index)


    def modify_1(self, root, index, value):
        if root == None:
            return
        if index > root.end or index < root.start:
            return
        if index == root.start and index == root.end:
            root.max = value
        self.modify_1(root.left, index, value)
        self.modify_1(root.right, index, value)

    def update(self, root, index):
        if root == None or root.left == None or root.right == None:
            return
        if index > root.end or index < root.start:
            return
        self.update(root.left, index)
        self.update(root.right, index)
        root.max = max(root.left.max, root.right.max)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值