lintcode:线段树的修改

线段树的修改

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

设计一个 modify 的方法,接受三个参数 root、 index 和 value。该方法将 root 为跟的线段树中 [startend] = [indexindex] 的节点修改为了新的 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]
解题

递归
/**
 * 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, index, value: The root of segment tree and 
     *@ change the node's value with [index, index] to the new given value
     *@return: void
     */
    public void modify(SegmentTreeNode root, int index, int value) {
        // write your code here
         if(root.start==root.end){  
            if(root.start==index)  
                root.max=value;  
            return ;  
        }  
          
        modify(root.left,index,value);  
        modify(root.right,index,value);  
          
        root.max = Math.max(root.left.max,root.right.max);  
    }
}
Java Code
"""
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
        if root == None:
            return 
        if root.start == root.end:
            if root.start == index:
                root.max = value
            return 
        self.modify(root.left,index,value)
        self.modify(root.right,index,value)
        root.max = max(root.left.max,root.right.max)
        
Python Code
原文地址:https://www.cnblogs.com/theskulls/p/5284836.html