203. Segment Tree Modify
最后更新
二刷
08-Jan-2017
利用线段树来更改,找到更改的NODE,然后更改那个brach上的所有max值。
首先确定recursion的终止条件
然后通过判断大小来找方向
找到NODE之后post order来进行更改。
public class Solution {
public void modify(SegmentTreeNode root, int index, int value) {
if (root == null) return;
if (index < root.start || index > root.end) return;
if (root.start == index && root.end == index) {
root.max = value;
} else {
int mid = root.start + (root.end - root.start) / 2;
if (index <= mid) {
modify(root.left, index, value);
} else {
modify(root.right, index, value);
}
root.max = Math.max(root.right.max, root.left.max);
}
}
}
看了下别人的做法,自己做的时候没想清楚。
一方面想判断走向来只遍历一边,一方面又添加了终止条件。
实际上如果添加终止条件,可以两边都走,走错的话会因为终止条件直接return。。但是毕竟多进了一个循环,所以时间慢一点点点点点
public class Solution {
public void modify(SegmentTreeNode root, int index, int value) {
if (root == null) return;
if (root.start > index || root.end < index) return;
if (root.start == index && root.end == index) {
root.max = value;
} else {
modify(root.left, index, value);
modify(root.right, index, value);
root.max = Math.max(root.left.max, root.right.max);
}
}
}
203. 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 Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- 『线段树 Segment Tree』
更新了基础部分 更新了\(lazytag\)标记的讲解 线段树 Segment Tree 今天来讲一下经典的线段树. 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间 ...
- 关于线段树的感悟(Segment Tree)
线段树的感悟 : 学过的东西一定要多回头看看,不然真的会忘个干干净净. 线段树的 Introduction : English Name : Segment Tree 顾名思义 : 该数据结构由两个重 ...
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- [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 ...
- Segment Tree Query I & II
Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
随机推荐
- HDU 1702 http://acm.hdu.edu.cn/showproblem.php?pid=1702
#include<stdio.h> #include<string.h> #include<queue> #include<stack> #define ...
- Umbraco官方技术文档 中文翻译
Umbraco 官方技术文档中文翻译 http://blog.csdn.net/u014183619/article/details/51919973 http://www.cnblogs.com/m ...
- 苹果iOS锁屏制作
下面我们开始. 一.锁屏界面 可以观察到,iphone的锁屏界面在时间和解锁部分有着透明强高光风格的背景,高光部分有非常明显的界限,边缘部分1像素的高光也是非常醒目的,整体感觉整个表面非常光滑,如同玻 ...
- SQLite的37个核心函数
转载:http://www.feiesoft.com/00012/ abs(X) abs(X)返回 X 的绝对值. Abs(X) returns NULL if X is NULL. Abs(X) r ...
- VS清除缓存
今天不小心在项目里面把一个 == 写成了 =,结果数据一下子崩溃了. 后来测试,发现,换一个编译环境,或者换一个编译模式比如debug改成release,就好使了. 1 测试流程 2 测试数据 3 ...
- ThinkPHP3.1.3的单字母函数汇总
A函数: 用于实例化Action 格式:[项目://][分组/]模块 /** * A函数用于实例化Action 格式:[项目://][分组/]模块 * @param string $name Acti ...
- SSH登录失败:Host key verification failed
转载自:https://help.aliyun.com/knowledge_detail/41471.html 注意:本文相关 Linux 配置及说明已在 CentOS 6.5 64 位操作系统中进行 ...
- wiki1169-传纸条(dp)
http://wikioi.com/problem/1169/ 四维数组和三维数组: #include<iostream> #include<cstdio> #include& ...
- android 用ListView实现表格样式
原文:http://blog.csdn.net/centralperk/article/details/8016350 效果图: 源码下载地址:http://download.csdn.net/det ...
- MySQL · BUG分析 · Rename table 死锁分析
http://mysql.taobao.org/monthly/2016/03/06/ 背景 InnoDB buffer pool中的page管理牵涉到两个链表,一个是lru链表,一个是flush 脏 ...