The structure of Segment Tree is a binary tree which each node has two attributes startand 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 bybuild method.
  • The left child of node A hasstart=A.left, end=(A.left + A.right) / 2.
  • The right child of node A hasstart=(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.

Have you met this question in a real interview?

Yes
Clarification

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

Example

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;
}
};

类似题目:

Segment Tree Build

[LintCode] Segment Tree Build II 建立线段树之二的更多相关文章

  1. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  2. Lintcode: Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

  3. Lintcode: Segment Tree Build

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  4. 439. Segment Tree Build II

    最后更新 08-Jan-2017 开始介绍线段树的主要作用了,可以快速在区间查找极值,我猜是这样的..... 一个NODE的最大值取决于它左边和右边最大值里大 按个,所以,所以什么?对了,我们该用po ...

  5. [学习笔记]Segment Tree Beats!九老师线段树

    对于这样一类问题: 区间取min,区间求和. N<=100000 要求O(nlogn)级别的算法 直观体会一下,区间取min,还要维护区间和 增加的长度很不好求.... 然鹅, 从前有一个来自杭 ...

  6. Segment Tree Build I & II

    Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...

  7. ACM学习历程——POJ3321 Apple Tree(搜索,线段树)

          Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...

  8. lintcode :Segmemt Tree Build II

    题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...

  9. 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 ...

随机推荐

  1. PMP 第二章 项目生命周期与组织

    1 项目组织机构类型有哪些? 区别是什么? 职能型  矩阵型  项目性 2 什么是事业环境因素? 什么是组织过程资产? 如何区分事业环境因素和组织过程资产? 事业环境因素:事业环境因素指围绕项目或能影 ...

  2. Unity3D项目开发一点经验

    我们主要使用3dsmax2010进行制作,输出FBX的类型导入Unity3D中.默认情况下,3dsmax8可以和U3D软件直接融合,自动转换为FBX物体. 注意事项如下: 1.面数控制 在MAX软件中 ...

  3. C语言函数的读写

    文件打开关闭函数:fopen()和fclose() <FILE *fopen(char *filename, char *mode)| int fclose(FILE *fp)> 字符读写 ...

  4. MongoDB学习(1)—在Windows系统中安装MongoDB

    概述 本文主要介绍在Windows系统安装MongoDB的方法. MongoDB官方网址:http://www.mongodb.org/,最新版本为2.6.7. 注意: 从2.2版本开始,MongoD ...

  5. Vijos P1459 车展 treap求任意区间中位数

    描述 遥控车是在是太漂亮了,韵韵的好朋友都想来参观,所以游乐园决定举办m次车展.车库里共有n辆车,从左到右依次编号为1,2,…,n,每辆车都有一个展台.刚开始每个展台都有一个唯一的高度h[i].主管已 ...

  6. 简单解释Windows如何使用FS段寄存器

    详见附件 jpg改rar

  7. ubuntu14使用qemu调试linux内核

    # 下载内核源代码编译内核 cd ~/LinuxKernel/ wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.6.tar.x ...

  8. form表单验证和事件

    1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...

  9. ffmpeg处理RTMP流媒体的命令 发送流媒体的命令(UDP,RTP,RTMP)

    将文件当做直播送至live ffmpeg -re -i localFile.mp4 -c copy -f flv rtmp://server/live/streamName   re限制输出速率,按照 ...

  10. SpringMVC解析5-DispatcherServlet逻辑细节

    MultipartContent类型的request处理 对于请求的处理,spring首先考虑的是对于Multipart的处理,如果是MultipartContent类型的request,则转换req ...