先更新对应节点的值,再遍历一次修改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)