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 ...
随机推荐
- 汇总:Linux下svn命令大全
svn(subversion)是近年来崛起的版本管理工具,svn服务器有2种运行方式:独立服务器和借助apache.2种方式各有利弊.不管是那种方式,都需要使用各种命令来实现.在本文中,haohtml ...
- UVaLive 7371 Triangle (水题,判矩形)
题意:给定两个三角形,问你能不能拼成矩形. 析:很明显,要想是矩形,必须是四个角是直角,那么三角形必须是直角三角形,然后就是只能斜边相对,然后呢?就没了. 代码如下: #pragma comment( ...
- WinAPI: FindWindow、FindWindowEx - 查找窗口
FindWindow( lpClassName, {窗口的类名} lpWindowName: PChar {窗口的标题} ): HWND; {返回窗口的 ...
- 关于mysql_fetch_****
今天调试如下代码: mysql_select_db('content',$link);//选择数据库 mysql_query("set names utf8");//设置编码格式 ...
- iOS类别(Category)
iOS类别(Category)与扩展(Extension) 苹果的官方文档 Category在iOS开发中使用非常频繁.尤其是在为系统类进行拓展的时候,我们可以不用继承系统类,直接给系统类添加方法,最 ...
- 详解Oracle创建用户权限全过程
本文将介绍的是通过创建一张表,进而实现Oracle创建用户权限的过程.以下这些代码主要也就是为实现Oracle创建用户权限而编写,希望能对大家有所帮助. 注意:每条语语分开执行,结尾必须用分号; // ...
- C# 常用日期类型转换帮助类
本文转载:http://www.cnblogs.com/iamlilinfeng/p/3378659.html 最近工作比较忙,与此同时自己也在业余时间开发一个电子商务网站.虽然每天都很累,但感觉过的 ...
- SSL握手过程
原文地址: http://my.oschina.net/u/1188877/blog/164982 一.SSL握手有三个目的:1. 客户端与服务器需要就一组用于保护数据的算法达成一致:2. 它们需要确 ...
- 搭建Nginx图片服务器
搭建Nginx图片服务器 Part-I 安装Nginx 安装PCRE 下载 ngx_cache_purge 并解压,用来清除缓存 下载Nginx并解压 cd nginx-1.7.7 编译,--pref ...
- [GIF] The Phase Property in GIF Loop Coder
In this lesson, we look at one of the most powerful features in GIF Loop Coder, the phase property, ...