[LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes start
and end
denote an segment / interval.
start and end are both integers, they should be assigned in following rules:
- The root's start and end is given by
build
method. - The left child of node A has
start=A.left, end=(A.left + A.right) / 2
. - The right child of node A has
start=(A.left + A.right) / 2 + 1, end=A.right
. - if start equals to end, there will be no children for this node.
Implement a build
method with a given array, so that we can create a corresponding segment tree with every node value represent the corresponding interval max value in the array, return the root of this segment tree.
Segment Tree (a.k.a Interval Tree) is an advanced data structure which can support queries like:
- which of these intervals contain a given point
- which of these points are in a given interval
See wiki:
Segment Tree
Interval Tree
Given [3,2,1,4]
. The segment tree will be:
[0, 3] (max = 4)
/ \
[0, 1] (max = 3) [2, 3] (max = 4)
/ \ / \
[0, 0](max = 3) [1, 1](max = 2)[2, 2](max = 1) [3, 3] (max = 4)
这道题是之前那道Segment Tree Build的拓展,这里面给线段树又增添了一个max变量,然后让我们用一个数组取初始化线段树,其中每个节点的max为该节点start和end代表的数组的坐标区域中的最大值。建树的方法跟之前那道没有什么区别,都是用递归来建立,不同的地方就是在于处理max的时候,如果start小于end,说明该节点还可以继续往下分为左右子节点,那么当前节点的max就是其左右子节点的max的较大值,如果start等于end,说明该节点已经不能继续分了,那么max赋为A[left]即可,参见代码如下:
- class Solution {
- public:
- /**
- *@param A: a list of integer
- *@return: The root of Segment Tree
- */
- SegmentTreeNode * build(vector<int>& A) {
- return build(A, , A.size() - );
- }
- SegmentTreeNode* build(vector<int>& A, int start, int end) {
- if (start > end) return NULL;
- SegmentTreeNode *node = new SegmentTreeNode(start, end);
- if (start < end) {
- node->left = build(A, start, (start + end) / );
- node->right = build(A, (start + end) / + , end);
- node->max = max(node->left->max, node->right->max);
- } else {
- node->max = A[start];
- }
- return node;
- }
- };
类似题目:
[LintCode] Segment Tree Build II 建立线段树之二的更多相关文章
- [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 Build
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- 439. Segment Tree Build II
最后更新 08-Jan-2017 开始介绍线段树的主要作用了,可以快速在区间查找极值,我猜是这样的..... 一个NODE的最大值取决于它左边和右边最大值里大 按个,所以,所以什么?对了,我们该用po ...
- [学习笔记]Segment Tree Beats!九老师线段树
对于这样一类问题: 区间取min,区间求和. N<=100000 要求O(nlogn)级别的算法 直观体会一下,区间取min,还要维护区间和 增加的长度很不好求.... 然鹅, 从前有一个来自杭 ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- ACM学习历程——POJ3321 Apple Tree(搜索,线段树)
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...
- lintcode :Segmemt Tree Build II
题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...
- 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 ...
随机推荐
- 【译】安装Sonar要求
本文仅为本人看sonar官方文档时,因其为英文,故简单整理翻译[英语不好,见谅!] http://docs.sonarqube.org/display/SONAR/Requirements 目录 ...
- Apache与Tomcat联系及区别(转)
Apache与Tomcat都是Apache开源组织开发的用于处理HTTP服务的项目,两者都是免费的,都可以做为独立的Web服务器运行.Apache是Web服务器而Tomcat是Java应用服务器. A ...
- 深入解析结构化异常处理(SEH)
jpg 改 rar
- memcached for windows 修改端口和最大内存,以及常用命令
在windows中使用memcached,必须先下载memcached for win32安装. PHP模块MemCache下载地址:http://downloads.php.net/pierre 服 ...
- Font Awesome符号字体
http://www.fontawesome.com.cn/ 引用CSS包之后根据图标库找到所需的图标代码 使用i标签或者a标签皆可,符号为文字性质,可以直接通过修改text颜色从而修改符号颜色
- AChartEngine使用View显示图表
学习过AChartEngine的人肯定都知道,使用ChartFactory创建一张图表可以使用Intent方法,之后调用StartActivity来启用这个Intent,但是这么左右一个坏处,就是当你 ...
- vs 中怎么用c改变部分字体颜色
// test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> #include< ...
- http://blog.csdn.net/u010246789/article/details/52539576
http://blog.csdn.net/u010246789/article/details/52539576
- Spring3.0 demo (注解自动注入)
这个demo是maven工程,目录结构如下 pom.xml maven依赖 .....省略 <dependency> <groupId>org.springframework& ...
- java代码获知该方法被哪个类、哪个方法、在哪一行调用
public class TestMain { public static void main(String[] args){ hello(); } public static void hello( ...