Lintcode: Segment Tree Modify
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的更多相关文章
- Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- 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 ...
- Lintcode: Segment Tree Build
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- 203. Segment Tree Modify
最后更新 二刷 08-Jan-2017 利用线段树来更改,找到更改的NODE,然后更改那个brach上的所有max值. 首先确定recursion的终止条件 然后通过判断大小来找方向 找到NODE之后 ...
- 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 ...
- 『线段树 Segment Tree』
更新了基础部分 更新了\(lazytag\)标记的讲解 线段树 Segment Tree 今天来讲一下经典的线段树. 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间 ...
随机推荐
- Simple Web Example
eclipse3.7 运行一个简单的servlet,Target Platfrom 必要的jar为 0 ACTIVE org.eclipse.osgi_3.7.2.v20120110-141 ...
- ::after,::before使用
::after,::before使用 1.:before 选择器在被选元素的内容前面插入内容. 请使用 content 属性来指定要插入的内容. <!DOCTYPE html> < ...
- 一次性搞清楚equals和hashCode
前言 在程序设计中,有很多的“公约”,遵守约定去实现你的代码,会让你避开很多坑,这些公约是前人总结出来的设计规范. Object类是Java中的万类之祖,其中,equals和hashCode是2个非常 ...
- angularJS平行控制器间共享数据
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- android通过pc脚本执行sqlite3脚本
最近在调研市面上的一些android db框架,需要经常重复的输入一堆比如 adb shell cd /data/data/com.example.testandroiddb/databases sq ...
- response.setCharacterEncoding方法未定义
代码一搬家,就报这错.之前几次稀里糊涂搞好忘记总结. 问题原因: 项目中用到Tomcat和weblogic.jar包,先引入weblogic.jar包时,HttpServletResponse下的se ...
- linux 开机启动设置
操作系统:Ubuntu12.04硬件环境:HP CQ45 当用户使用sudo apt-get install安装完apache和mysql之后,这些服务默认是开机启动的,但是有的时候需要 ...
- recordcount
rs.recordcount 有时不能取到数,这时 要更改游标为客户端游标 .
- Selenium2学习-004-WebUI自动化实战实例-002-百度登录
此文主要通过 百度登录 功能,进行 Selenium2 的实战实例讲解.文中所附源代码于 2015-01-17 23:33 亲测通过,敬请亲们阅览.同时,您也可参考此文进行其他网站(例如 京东.易迅. ...
- net异步线程注意事项
某人(不是我写的)写的异步代码:(还请博友们点评哈~) AsyncEventHandler[] asy = new AsyncEventHandler[ResultCount]; IAsyncRe ...