[LintCode] Segment Tree Build II 建立线段树之二

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, 0, A.size() - 1);
    }
    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) / 2);
            node->right = build(A, (start + end) / 2 + 1, end);
            node->max = max(node->left->max, node->right->max);
        } else {
            node->max = A[start];
        }
        return node;
    }
};

类似题目:

Segment Tree Build

时间: 2024-10-20 06:52:45

[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 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 meth

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 meth

Lintcode: Segment Tree Build

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 meth

Segment Tree Build I &amp; II

Segment Tree Build I 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 i

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

      Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by bran

HDU4509-湫湫系列故事——减肥记II(线段树)

湫湫系列故事--减肥记II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 2395    Accepted Submission(s): 1018 Problem Description 虽然制定了减肥食谱,但是湫湫显然克制不住吃货的本能,根本没有按照食谱行动! 于是,结果显而易见- 但是没有什么能难倒高智商美女湫湫的,她决定另寻对策

spoj gss2 : Can you answer these queries II 离线&amp;&amp;线段树

1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse to write two program of same kind at all. So he always failes in co

Lintcode: Segment Tree Modify

For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in this node's interval. Implement a modify function with three parameter root, index and value to change the node's value with [start, end] = [index, index

论线段树:二

<论线段树> ——线段树精细讲解第二篇   ——Yeasion_Nein出品 ><论线段树:一> ><论线段树:二> 在上一节中,我们大概了解了线段树的运算机制以及基本的代码运行 ,在这一节,我们主要针对一个例题进行更深层次的讲解. 首先来看一道题.(题目原链接:线段树2 [模板]线段树 2 题目描述 如题,已知一个数列,你需要进行下面三种操作: 1.将某区间每一个数乘上x 2.将某区间每一个数加上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第