<LeetCode> 题203:线段树的修改
来源:互联网 发布:软件开发图标素材 编辑:程序博客网 时间:2024/06/10 04:26
题目描述:
对于一棵 最大线段树, 每个节点包含一个额外的 max 属性,用于存储该节点所代表区间的最大值。
设计一个 modify 的方法,接受三个参数 root、 index 和value。该方法将 root 为根的线段树中[start, end] = [index, index]的节点修改为了新的 value ,并确保在修改后,线段树的每个节点的 max 属性仍然具有正确的值。
例如:
对于线段树:
如果调用 modify(root, 2, 4), 返回:
或 调用 modify(root, 4, 0), 返回:
数据结构:
/** * Definition of SegmentTreeNode: * class SegmentTreeNode * { * public: * int start, end, max; * SegmentTreeNode *left, *right; * SegmentTreeNode(int start, int end, int max) * { * this->start = start; * this->end = end; * this->max = max; * this->left = this->right = NULL; * } * } **/
代码:
class Solution {public: void modify(SegmentTreeNode *root, int index, int value) { if(root == NULL) { return; } if(root->start==root->end) { if(root->start==index) { root->max=value; } return ; } modify(root->left,index,value); modify(root->right,index,value); root->max = max(root->left->max, root->right->max); }};
0 0
- <LeetCode> 题203:线段树的修改
- 线段树的修改
- 线段树的修改
- 线段树的修改
- lintcode-线段树的修改-203
- <LeetCode> 题201:线段树的构造
- <LeetCode> 题202:线段树的查询
- 线段树的区间修改
- LintCode:线段树的修改
- 线段树的修改-LintCode
- <LeetCode> 题439:线段树的构造(2)
- <LeetCode> 题247:线段树的查询(2)
- hihoCoder 1078 线段树的区间修改
- hihoCoder 1078 : 线段树的区间修改
- hiho1078 线段树的区间修改
- 解题报告:线段树的修改
- hihocoder#1078 线段树的区间修改
- hihoCoder 1078 线段树的区间修改
- ini读写
- 牛客网刷题知识点
- 快速排序
- CSS—实现悬浮层
- NIO实战(二)——java.nio.ByteBuffer中flip、rewind、clear方法的区别
- <LeetCode> 题203:线段树的修改
- Spring AOP 常用的四种实现方式
- C++里的CString转成String怎么弄?大家都知道吗
- 呓语
- 河南多校大一训练赛
- [首次分析]PHP写框架
- 【leetcode Database】184. Department Highest Salary
- Service的子类IntentService(三)
- Servlet跳转到jsp页面的几种方法