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 method.
The left child of node A has start=A.left, end=(A.left + A.right) / 2.
The right child of node A has start=(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 start and 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
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]
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
/**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end) {
* this.start = start, this.end = end;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
*@param start, end: Denote an segment / interval
*@return: The root of Segment Tree
*/
public SegmentTreeNode build(int start, int end) {
// write your code here
if (start > end) return null;
if (start == end) return new SegmentTreeNode(start, start);
SegmentTreeNode cur = new SegmentTreeNode(start, end);
cur.left = build(cur.start, (cur.start+cur.end)/2);
cur.right = build((cur.start+cur.end)/2+1, cur.end);
return cur;
}
}
Lintcode: Segment Tree Build的更多相关文章
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- Lintcode: Segment Tree Query
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...
- LintCode "Expression Tree Build"
Lesson learnt: for any calculator problems, keep 2 stacks: 1 for operators and 1 for operands. class ...
- 439. Segment Tree Build II
最后更新 08-Jan-2017 开始介绍线段树的主要作用了,可以快速在区间查找极值,我猜是这样的..... 一个NODE的最大值取决于它左边和右边最大值里大 按个,所以,所以什么?对了,我们该用po ...
- 201. Segment Tree Build
最后更新 二刷 08-Jan-2017 一刷忘了什么时候做的,只是觉得这几个题挺好的,一步一步手动构建线段树,最终理解了这个数据结构,并且后面有利用的地方. 其实重要的3个东西题目已经提供了: 1) ...
随机推荐
- Cluster analysis
https://en.wikipedia.org/wiki/Cluster_analysis Cluster analysis or clustering is the task of groupin ...
- 7添加一个“X”到HTML:转到XHTML
XHTML中的X代表extensible,是以XML为基础的另一种说法.XML表示可扩展的标记语言. XML是一种可以用来开发新的标记语言的语言,而HTML只是一门标记语言. HTML转化为XHTML ...
- CSS :before和:after (转)
前几天的晚上较全面的去看了下css的一些文档和资料,大部分的样式运用都没什么大问题了,只是有些许较陌生,但是也知道他们的存在和实现的是什么样式.今天主要想在这篇学习笔记中写的也不多,主要是针对:bef ...
- 【Android测试】【随笔】在手机里用命令行创建中文文件夹
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4580319.html 不知道为啥当时自己写了一段在手机里用 ...
- Solr分页与高亮(使用SolrNet实现)
Solr分页与高亮(使用SolrNet实现) 本节我们使用Asp.net MVC实现Solr客户端查询,建议使用SolrNet这个客户端,开源地址在:https://github.com/mausch ...
- QGridLayout--01
#include "mainwindow.h" #include <QApplication> #include<QLabel> #include<Q ...
- 1011 最大公约数GCD
1011 最大公约数GCD 基准时间限制:1 秒 空间限制:131072 KB 输入2个正整数A,B,求A与B的最大公约数. Input 2个数A,B,中间用空格隔开.(1<= A,B < ...
- 网络拥塞控制(三) TCP拥塞控制算法
为了防止网络的拥塞现象,TCP提出了一系列的拥塞控制机制.最初由V. Jacobson在1988年的论文中提出的TCP的拥塞控制由“慢启动(Slow start)”和“拥塞避免(Congestion ...
- The Bip Buffer - The Circular Buffer with a Twist
Introduction The Bip-Buffer is like a circular buffer, but slightly different. Instead of keeping on ...
- HTML-002-弹出对话框
日常的网页编程中,弹出对话框经常会以各种形式出现,例如:信息提示框.确认框.新增.修改信息等对话框均是其不同的表现形式. 此文以弹出信息新增对话框进行简要演示,经请参阅! 以下为其对应的结构目录: a ...