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.

Have you met this question in a real interview? Yes
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]
Note
We suggest you finish problem Segment Tree Build and Segment Tree Query first. Challenge
Do it in O(h) time, h is the height of the segment tree.
 /**
* 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) {
root.max = value;
return;
}
int mid = (root.start + root.end)/2;
if (index <= mid) modify(root.left, index, value);
else modify(root.right, index, value);
root.max = Math.max(root.left.max, root.right.max);
}
}

Lintcode: Segment Tree Modify的更多相关文章

  1. Segment Tree Modify

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

  2. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  3. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  4. Lintcode: Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

  5. Lintcode: Segment Tree Query

    For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...

  6. Lintcode: Segment Tree Build

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  7. 203. Segment Tree Modify

    最后更新 二刷 08-Jan-2017 利用线段树来更改,找到更改的NODE,然后更改那个brach上的所有max值. 首先确定recursion的终止条件 然后通过判断大小来找方向 找到NODE之后 ...

  8. Lintcode247 Segment Tree Query II solution 题解

    [题目描述] For an array, we can build a Segment Tree for it, each node stores an extra attribute count t ...

  9. 『线段树 Segment Tree』

    更新了基础部分 更新了\(lazytag\)标记的讲解 线段树 Segment Tree 今天来讲一下经典的线段树. 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间 ...

随机推荐

  1. LR脚本技巧

    1.参数化空值       如上图所示,当参数化时某个值需要为空值(非空格),直接在参数化文件中空一行/格即可,虽然Parameter List界面上没有显示空的那一行,但并不影响取值. 2.手工日志 ...

  2. MP20 MBO issue summary

    MP3 MBO经验,教训 改名字HGA003_PTOT_01,发现居然闪现进度条,正常情况是不会闪现进度条的,只是改个名字而已,怀疑之前用过这个名字,所以我后来改成HGA003_PTOT_03了. 先 ...

  3. 发现美的眼睛 Prepared SQL Statement

    DROP PROCEDURE IF EXISTS truncate_insert_sales_rank_toparow_month; DELIMITER /w/ CREATE PROCEDURE tr ...

  4. php mysql 事务处理

    MYSQL 的事务处理主要有两种方法. 1 .用 begin,rollback,commit 来实现 begin 开始一个事务 rollback 事务回滚 commit 事务确认    2 .直接用  ...

  5. 【No.4 Ionic】修改 cordova 插件

    在使用 cordova 过程 使用的插件 有可能不能满足个人需求,就需要修改,下面就直接说说步骤 插件结构 我用 cordova-plugin-inappbrowser 插件 讲解 在目录中有个 sr ...

  6. Python学习笔记——1——基础知识

    1.1.变量和算法 python语言很类似人类语言,变量不需要定义类型.比如: 整型 字符串类型 数组 Java int a=12 String s="test" String[] ...

  7. Java回调实现

    一.回调的形式 1. C.C++和Pascal允许将函数指针作为参数传递给其它函数.JavaScript,Python,和PHP允许简单的将函数名作为参数传递. 2. .NET Framework的语 ...

  8. Prism&MEF构建开发框架 (一)

    Shell框架XECA shell.xaml主要起到是一个容器或壳的作用 <Window x:Class="XECA.Shell"      xmlns="http ...

  9. zepto源码--classRE、maybeAddPx、children、defaultDisplay--学习笔记

    1.classRE 对获取className的操作,进行缓存.如果缓存中有,直接读取缓存中的值,如果没有,则先进行缓存的存储,再读取值. 利用前面变量定义的classCache={}进行缓存的操作,如 ...

  10. interview review

    缘起: 因为最近要找工作,自己总结了一下面试的注意事项. 1自我介绍方法 1.基本情况:姓名.年龄.学历.家庭与理想. 简单明了,不要啰嗦. 2.学习能力:专业知识.勤奋好学. 用事实说明学习能力不错 ...