Segment Tree Build I & II
Segment Tree Build I
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 andend value, return the root of this segment tree.
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
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]
/**
* 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) {
if (start > end ) return null; if (start == end) {
return new SegmentTreeNode(start, start);
} else {
SegmentTreeNode root = new SegmentTreeNode(start, end);
root.left = build(start, (start + end) / );
root.right = build((start + end) / + , end);
return root;
}
}
}
Segment Tree Build II
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 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.
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
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)
/**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end, max;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end, int max) {
* this.start = start;
* this.end = end;
* this.max = max
* this.left = this.right = null;
* }
* }
*/
public class Solution { public SegmentTreeNode build(int[] A) {
if (A == null || A.length == ) return null; return build(A, , A.length - );
} public SegmentTreeNode build(int[] A, int start, int end) {
if (start > end) return null; if (start == end) {
return new SegmentTreeNode(start, end, A[start]);
} else {
SegmentTreeNode root = new SegmentTreeNode(start, end, );
root.left = build(A, start, (start + end) / );
root.right = build(A, (start + end) / + , end);
root.max = Math.max(root.left.max, root.right.max);
return root;
}
}
}
Segment Tree Build I & II的更多相关文章
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- Segment Tree Query I & II
Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Lintcode: Segment Tree Build
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- 439. Segment Tree Build II
最后更新 08-Jan-2017 开始介绍线段树的主要作用了,可以快速在区间查找极值,我猜是这样的..... 一个NODE的最大值取决于它左边和右边最大值里大 按个,所以,所以什么?对了,我们该用po ...
- 201. Segment Tree Build
最后更新 二刷 08-Jan-2017 一刷忘了什么时候做的,只是觉得这几个题挺好的,一步一步手动构建线段树,最终理解了这个数据结构,并且后面有利用的地方. 其实重要的3个东西题目已经提供了: 1) ...
- Lintcode247 Segment Tree Query II solution 题解
[题目描述] For an array, we can build a Segment Tree for it, each node stores an extra attribute count t ...
- 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 :Segmemt Tree Build II
题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...
随机推荐
- Tomcat+eclipse JSP windows开发环境配置
一.安装Java SE http://www.oracle.com/technetwork/java/javase/downloads/index.html ,配置JAVA_HOME环境变量 二.安装 ...
- 【CodeForces 625C】K-special Tables
题意 把1到n*n填在n*n的格子里.要求每一行都是递增的,使第k列的和最大. 分析 第k列前的格子1 2 .. 按要求填到满格,然后第k列及后面的格子,都从左到右填递增1的数. 第k列的和再加起来, ...
- bzoj 3437 斜率优化DP
写题解之前首先要感谢妹子. 比较容易的斜率DP,设sum[i]=Σb[j],sum_[i]=Σb[j]*j,w[i]为第i个建立,前i个的代价. 那么就可以转移了. /**************** ...
- POJ1089 Intervals
Description There is given the series of n closed intervals [ai; bi], where i=1,2,...,n. The sum of ...
- 轻量级应用开发之(04)UIScrollView-1
本文是我在学习OC中的一些经验总结,在学习中总结了常用的Mac技巧,欢迎群友对本文提出意见,如有问题请联系我. 一 什么是UIScrollView 1)移动设备的屏幕大小是极其有限的,因此直接展示在用 ...
- HDU2196computer(树上最远距离 + DP)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 采用httpclient提交数据到服务器
1)Get提交数据 效果演示:
- anr产生的原理&如何避免(android)
- params参数的调用
namespace params参数的用法 { class Program { public static void Test(string name,params int[] score) { ; ...
- dedecms网站栏目增加缩略图的方法-测试通过
有时候因为网站功能需求,我们需要为织梦程序的栏目页添加缩略图功能,这里有一个栏目添加缩略图的方法,供大家参考 涉及到文件如下(注意备份): dede/catalog_add.php dede/cata ...