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. 注解:【基于外键的】Hibernate单向1->1关联

    Person与Address关联:单向1->1,[基于外键的] Person.java package org.crazyit.app.domain; import javax.persiste ...

  2. waterMarkTextBox

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  3. N种内核注入DLL的思路及实现

    内核注入,技术古老但很实用.现在部分RK趋向无进程,玩的是SYS+DLL,有的无文件,全部存在于内存中.可能有部分人会说:"都进内核了.什么不能干?".是啊,要是内核中可以做包括R ...

  4. scrollTo 和 scrollBy

      涉及到滑动,就涉及到VIEW,大家都知道,Android的UI界面都是由一个一个的View以及View的派生类组成,View作为基类,而常用的布局里面的各种布局就是它派生出来的ViewGroup的 ...

  5. SQL SERVER数据库的表中修改字段的数据类型后,不能保存

      在数据库里面建了一个表,可是由于对SQL SERVER的建表功能不熟悉,不知道把主键设成什么是好,就先设置了个TEXT类型,可是后来朋友们告诉我说,TEXT类型容易让数据文件变得很大,还 是改成一 ...

  6. 让Web API支持$format参数的方法

    public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web AP ...

  7. spring实例教程

    1.配置好spring mvc发现访问无法匹配,很可能是文件放的位置或者相对目录不对. 2.实例大全:http://www.yiibai.com/spring/spring-collections-l ...

  8. mob免费短信验证码安卓SDK调用方法

    很不错的一款免费验证码平台,支持IOS.安卓,比那些收费的稳定.开发容易.分享给大家,希望大家不要滥用 官网: http://mob.com/ 官方有开发文档,但是有几点要注意的官方没有提到,导致初始 ...

  9. H5危险的文件上传对话框

    文件对话框 文件上传对话框是一直以来就存在的网页控件. 到了 HTML5 时代,增加了更多的功能,例如支持文件多选.Chrome 甚至还支持「上传文件夹」这一私有特征: <input type= ...

  10. css 让内容满屏居中不变形

    .selector { position: fixed; width: 100%; height: 100%; background-image: url(path); background-repe ...