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 ...
随机推荐
- C#类、静态类、静态变量,初始化执行顺序
执行顺序: 类的静态变量 ↓ 类的静态构造函数 ↓ 类的普通变量 ↓ 基类的静态变量 ↓ 基类的静态构造函数 ↓ 基类的普通变量 ↓ 基类的构造函数 ↓ 类的构造函数
- [C语言 - 7] 结构体struct
A. 基本知识 与数组的对比 数组: 构造类型 只能有多个相同类型的数据构成 结构体: 结构体类型 可以由多个不同类型的数据构成 1. 定义类型 struct Student { int ...
- UVaLive 7371 Triangle (水题,判矩形)
题意:给定两个三角形,问你能不能拼成矩形. 析:很明显,要想是矩形,必须是四个角是直角,那么三角形必须是直角三角形,然后就是只能斜边相对,然后呢?就没了. 代码如下: #pragma comment( ...
- setbuffer和freopen做一个简单的日志组件
目标场景是这样的: 多线程的应用程序要频繁打一些小字节的日志,也不想引用很重的日志库. 设想了一个极其简单的日志组件,main线程中重定向stdout到文件,同时setbuffer设置一个10k的缓冲 ...
- shell脚本的入参
shell脚本参数可以任意多,但只有前9个可以被访问,使用shift命令可以改变这个限制.参数从第一个开始,在第九个结束.$0 程序名字$n 第n个参数值,n=1..9 $* 所有命令行参数$@ ...
- Django搭建博客后台
转载自:http://sanwen8.cn/p/1cboypN.html 首先创建项目和blog应用: 在cmd命令行下,输入: django-admin.py startproject myblog ...
- cocos2d-x CCTableView
转自:http://www.cnblogs.com/dcxing/archive/2013/01/16/2862068.html CCTableView在游戏中一般用在背包这样场景或层中,当然也不止这 ...
- cocos2d-x CCListView
转自:http://blog.csdn.net/onerain88/article/details/7641126 cocos2d-x 2.0 版更新了,把opengl 1.1 替换为opengl 2 ...
- 我的第一个javascript网页作业
1: <html> 2: <title> 3: 4: </title> 5: <body> 6: <style type="text ...
- Excel设置数据有效性实现单元格下拉菜单的3种方法(转)
http://blog.csdn.net/cdefu/article/details/4129136 一.直接输入: 1.选择要设置的单元格,譬如A1单元格: 2.选择菜单栏的“数据”→“有效性”→出 ...