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

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

本文转自博客园Grandyang的博客,原文链接:建立线段树之二[LintCode] Segment Tree Build II ,如需转载请自行联系原博主。

相关文章
|
5月前
|
存储 算法 测试技术
数据结构学习记录——树习题-Complete Binary Search Tree(题目描述、输入输出示例、数据结构的选择、核心算法、计算左子树的规模)
数据结构学习记录——树习题-Complete Binary Search Tree(题目描述、输入输出示例、数据结构的选择、核心算法、计算左子树的规模)
77 1
|
机器学习/深度学习 存储 C++
【PAT甲级 - C++题解】1123 Is It a Complete AVL Tree
【PAT甲级 - C++题解】1123 Is It a Complete AVL Tree
100 0
|
机器学习/深度学习 C++
【PAT甲级 - C++题解】1099 Build A Binary Search Tree
【PAT甲级 - C++题解】1099 Build A Binary Search Tree
83 0
每日一题---21. 合并两个有序链表[力扣][Go]
每日一题---21. 合并两个有序链表[力扣][Go]
每日一题---21. 合并两个有序链表[力扣][Go]
每日一题---24. 两两交换链表中的节点[力扣][Go]
每日一题---24. 两两交换链表中的节点[力扣][Go]
每日一题---24. 两两交换链表中的节点[力扣][Go]
每日一题---23. 合并K个升序链表[力扣][Go]
每日一题---23. 合并K个升序链表[力扣][Go]
每日一题---23. 合并K个升序链表[力扣][Go]
牛客hot100--BM23---二叉树的前序遍历(简单难度)
牛客hot100--BM23---二叉树的前序遍历(简单难度)
128 0
牛客hot100--BM23---二叉树的前序遍历(简单难度)
牛客hot100--BM25---二叉树的后序遍历(简单难度)
牛客hot100--BM25---二叉树的后序遍历(简单难度)
84 0
牛客hot100--BM25---二叉树的后序遍历(简单难度)
|
测试技术
牛客hot100--BM17---二分查找I(简单难度)
牛客hot100--BM17---二分查找I(简单难度)
120 0
牛客hot100--BM17---二分查找I(简单难度)
|
人工智能
[Codeforces 1286B] Numbers on Tree | 技巧构造
Evlampiy was gifted a rooted tree. The vertices of the tree are numbered from 1 to n. Each of its vertices also has an integer ai written on it. For each vertex i, Evlampiy calculated ci — the number of vertices j in the subtree of vertex i, such that a j < a i
115 0
[Codeforces 1286B] Numbers on Tree | 技巧构造