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. JavaScript BOM对象介绍

    bom:即broswer object model(浏览器对象模型),由五个对象组成:        Window:对象表示浏览器中打开的窗口 最顶层对象.       Navigator :浏览器对 ...

  2. 用PHP链接mysql数据库

    PHP提供了两套数据库可用于访问mysql数据库 1)MySQL扩展函数数据库 2)MySQLI扩展数据库(improved) 使用MySQLI函数访问MySQL数据库步骤 1)链接数据库管理系统 m ...

  3. 利用JAX-WS 开发web服务

    近日在学习Rogers Candenhead的第六版的<Java 入门经典>第22章.利用JAX-WS 开发web服务,简略总结而言主要包括以下几个步骤: 1.定义服务端点接口类: 主要就 ...

  4. hdu1712 分组背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1712 题意:有n门课程,和m天时间,完成mp[i][j]得到的价值为第i行j列的数字,求最 ...

  5. 关于DOM

    前言 DOM的作用是将网页转为一个javascript对象,从而可以使用javascript对网页进行各种操作(比如增删内容).浏览器会根据DOM模型,将HTML文档解析成一系列的节点,再由这些节点组 ...

  6. configSections

         由于最近一个项目的数据库变动比较频繁, 为了减少数据层的负担, 打算采用.net的MVC框架, 使用LINQ对付数据层.       这个框架的web.config文件里出现了configS ...

  7. JS(获得当前时间并且用2015-01-01格式表示)

    一个简单的小例子,实现获得当前时间,js代码如下: function getdate() {var date = new Date();var mon = date.getMonth()  + 1;  ...

  8. MFC 单文档 根据数据 绘图

    以VS2015中创建SDI为例 选择生成的类为:C***View,基类为CView,***为项目名 在***Doc.h和***Doc.cpp中创建数据模型,在***View.cpp的OnDraw()中 ...

  9. POJ1637 Sightseeing tour(判定混合图欧拉回路)

    有向连通图存在欧拉回路的充要条件是所有点入度=出度. 首先随便给定所有无向边一个方向(不妨直接是u->v方向),记录所有点的度(记:度=入度-出度). 这时如果有点的度不等于0,那么就不存在欧拉 ...

  10. java 程序中添加socks 5代理

    在需要使用代理的地方添加如下code: System.getProperties().put("socksProxySet","true"); System.g ...