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) ...
随机推荐
- Andrew Ng机器学习公开课笔记 -- Mixtures of Gaussians and the EM algorithm
网易公开课,第12,13课 notes,7a, 7b,8 从这章开始,介绍无监督的算法 对于无监督,当然首先想到k means, 最典型也最简单,有需要直接看7a的讲义 Mixtures of G ...
- WCF中自定义消息编码器:压缩编码器的使用
通过抓包知道WCF在提交.返回数据的时候大多使用XML进行数据交互,如果返回DataTable那么这些数据将变得很大,通过查询找到一个对数据压缩的方法: http://msdn.microsoft.c ...
- php经典笔试题
五.基础及程序题(建议使用你擅长的语言:C/C++.PHP.Java) 5.写一个排序算法,可以是冒泡排序或者是快速排序,假设待排序对象是一个维数组.(提示:不能使用系统已有函数,另外请仔细回忆以前学 ...
- Android 一键直接查看Sqlite数据库数据
转自:http://www.cnblogs.com/trinea/archive/2012/11/16/2773656.html 本文主要介绍Android开发中如何一键直接查看sqlite数据库中的 ...
- 使用SecureCRT连接ubuntu
SecureCRT SSH2连接新装的UBUNTU 14.04 LTS 发现UBUNTU默认没有安装SSH 服务 在UBUNTU上 sudo apt-get install openssh-serve ...
- sqlserver 简单的事物用法
SELECT * FROM Interface_UserPort BEGIN TRY BEGIN TRAN Tran_2012_12_25 ,) --raiserror 50005N'抛出错误' CO ...
- HashSet HashTable HashMap的区别
(1)HashSet是set的一个实现类,hashMap是Map的一个实现类,同时hashMap是hashTable的替代品(为什么后面会讲到). (2)HashSet以对象作为元素,而HashMap ...
- C中文件操作说明
r 以只读方式打开文件,该文件必须存在. r+ 以读/写方式打开文件,该文件必须存在. rb+ 以读/写方式打开一个二进制文件,只允许读/写数据. rt+ 以读/写方式打开一个文本文件,允许读和写. ...
- PHP实现QQ第三方登录
PHP实现QQ第三方登录 学习之前,请大家先看一下oAuth协议. 首先呢,我们进入QQ互联的官方网站 http://connect.qq.com登入我们自己的QQ号,没有QQ号的小伙伴可以忽略本篇博 ...
- php--列表展示(小实训一月考)
效果图: