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 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 two parameters startand end, so that we can create a corresponding segment tree with every node has the correct start and end value, 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 start=0, end=3. The segment tree will be:

               [0,  3]
/ \
[0, 1] [2, 3]
/ \ / \
[0, 0] [1, 1] [2, 2] [3, 3]

Given start=1, end=6. The segment tree will be:

               [1,  6]
/ \
[1, 3] [4, 6]
/ \ / \
[1, 2] [3,3] [4, 5] [6,6]
/ \ / \
[1,1] [2,2] [4,4] [5,5]

这道题让我们建立线段树,也叫区间树,是一种高级树结构,但是题目中讲的很清楚,所以这道题实现起来并不难,我们可以用递归来建立,写法很简单,参见代码如下:

class Solution {
public:
/**
*@param start, end: Denote an segment / interval
*@return: The root of Segment Tree
*/
SegmentTreeNode * build(int start, int end) {
if (start > end) return NULL;
SegmentTreeNode *node = new SegmentTreeNode(start, end);
if (start < end) {
node->left = build(start, (start + end) / );
node->right = build((start + end) / + , end);
}
return node;
}
};

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

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

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  2. Lintcode: Segment Tree Build

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

  3. 2018.07.29~30 uoj#170. Picks loves segment tree VIII(线段树)

    传送门 线段树好题. 维护区间取两种最值,区间加,求区间两种历史最值,区间最小值. 自己的写法调了一个晚上+一个上午+一个下午+一个晚上并没有调出来,90" role="prese ...

  4. Wannafly Winter Camp 2020 Day 5C Self-Adjusting Segment Tree - 区间dp,线段树

    给定 \(m\) 个询问,每个询问是一个区间 \([l,r]\),你需要通过自由地设定每个节点的 \(mid\),设计一种"自适应线段树",使得在这个线段树上跑这 \(m\) 个区 ...

  5. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  6. 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)

    2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...

  7. Segment Tree Build I & II

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

  8. Lintcode: Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  9. HDU-2795Billboard+对宽度建立线段树

    参考:  https://blog.csdn.net/qiqi_skystar/article/details/49073309 传送门:http://acm.hdu.edu.cn/showprobl ...

随机推荐

  1. sqlserver 作业调度(作业常用的几个步骤)

    --[作业常用的几个步骤] EXEC msdb.dbo.sp_delete_job EXEC msdb.dbo.sp_add_job EXEC msdb.dbo.sp_add_jobstep EXEC ...

  2. LoadRunner录制图片验证码

    LoadRunner录制图片验证码 LoadRunner自身是无法捕获到图片验证码的,但是我们可以帮助LoadRunner来实现验证码的捕获. 1.图片验证码 图片验证码的产生来自服务器端,由服务器生 ...

  3. Tomcat不输入项目名进入自己项目(根目录指向自己的项目)

    <Host name="localhost" appBase="webapps" unpackWARs="true" autoDepl ...

  4. BZOJ 2565 回文串-Manacher

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2565 题意:中文题 思路:定义L[i],R[i].表示以i为左端点/右端点时,最长回文串长 ...

  5. theano报一种float类型错误的处理办法

    我实际用的环境是Keras,查错误时查到是Theano的配置问题,所以在标题里就写成Theano的问题了, 是这样的,从Github上下载的别人的代码,准备复现别人的实验,结果在机器上部署好环境之后跑 ...

  6. 移动端网页 -- 安卓与IOS兼容

    1.在a链接长按时,ios系统会识别并复制a链接中的href值,而安卓不会,只会选择复制文字 关于长按复制其他区域内容:pc端可以实现,在移动端目前还没有找到解决方案,很多都是基于flash的 2.i ...

  7. Spring Integration - 自动轮询发送手机短信

    Spring Integration 配置 <?xml version="1.0" encoding="UTF-8"?> <beans xml ...

  8. BFS(染色) LA 3977 Summits

    题目传送门 题意:题意坑爹.问符合条件的的山顶个数 分析:降序排序后从每个点出发,假设为山顶,如果四周的点的高度>h - d那么可以走,如果走到已经走过的点且染色信息(山顶高度)不匹配那么就不是 ...

  9. Hadoop中几个基本命令行命令

    对HDFS操作的几个命令(必须是Hadoop命令,否则只是对本地操作) 1. 查看HDFS中某个目录或文件的包含哪些文件和文件夹: bin/hadoop dfs/fs -ls 文件(夹)路径 若是非空 ...

  10. WPF DataGrid Control

    Introduction Since .NET 4.0, Microsoft is shipping a DataGrid control that provides all the basic fu ...