摘要:唯一需要注意的就是的賦值取左右子樹的的較大值,最后一層的獨(dú)立結(jié)點(diǎn)的為對應(yīng)數(shù)組中的值。
Segment Tree Build I Problem
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.
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
public class Solution { public SegmentTreeNode build(int start, int end) { // write your code here if (start > end) { return null; } SegmentTreeNode root = new SegmentTreeNode(start, end); if (start == end) { return root; } root.left = build(start, (start+end)/2); root.right = build((start+end)/2+1, end); return root; } }Segment Tree Build II Difference
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; } }Example
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)Note
唯一需要注意的就是max的賦值:取左右子樹的max的較大值,最后一層的獨(dú)立結(jié)點(diǎn)的max為對應(yīng)數(shù)組中的值。
Solutionpublic class Solution { public SegmentTreeNode build(int[] A) { // write your code here return build(A, 0, A.length - 1); } public SegmentTreeNode build(int[] A, int start, int end) { if (start > end) { return null; } SegmentTreeNode root = new SegmentTreeNode(start, end, Integer.MIN_VALUE); if (start != end) { int mid = (start + end) / 2; root.left = build(A, start, mid); root.right = build(A, mid+1, end); root.max = Math.max(root.left.max, root.right.max); } else root.max = A[end]; return root; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/65477.html
摘要:題目是要查詢到這個(gè)區(qū)間內(nèi)某一點(diǎn)的。值是從最底層的子節(jié)點(diǎn)值里取最大值。因此,不用太復(fù)雜,遞歸就可以了。與所不同的是,對所給區(qū)間內(nèi)的元素個(gè)數(shù)求和,而非篩選。這樣就會(huì)出現(xiàn)的情況,視作本身處理。 Segment Tree Query Problem For an integer array (index from 0 to n-1, where n is the size of this ar...
摘要:和其它題目一樣,依然是遞歸的做法。注意若樹的結(jié)點(diǎn)范圍為,那么至少有個(gè)數(shù)在左子樹上,所以語句里用了號。另外,最后一句是調(diào)用遞歸之后的結(jié)果,必須寫在最后面。 Problem For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in this nodes i...
摘要:這道題目是篩選出數(shù)組中的最小值,存入新數(shù)組。因此,聯(lián)想到和系列的題目,對于的處理,使用線段樹是非常有效的方法。之前我們創(chuàng)建的線段樹,有和兩個(gè)。參照這個(gè)參數(shù),可以考慮在這道題增加一個(gè)的參數(shù),代表每個(gè)結(jié)點(diǎn)的最小值。 Problem Given an integer array (index from 0 to n-1, where n is the size of this array),...
摘要:示例解題代碼注意,如果要在上提交解答,必須把接口和類的代碼一并提交,這里并沒有在寫類中 我理解的數(shù)據(jù)結(jié)構(gòu)(八)—— 線段樹(SegmentTree) 一、什么是線段樹 1.最經(jīng)典的線段樹問題:區(qū)間染色有一面墻,長度為n,每次選擇一段墻進(jìn)行染色,m次操作后,我們可以看見多少種顏色?m次操作后,我們可以在[i, j]區(qū)間內(nèi)看見多少種顏色?showImg(https://segmentfau...
閱讀 2323·2021-10-08 10:04
閱讀 1097·2021-09-03 10:40
閱讀 1150·2019-08-30 15:53
閱讀 3309·2019-08-30 13:13
閱讀 2925·2019-08-30 12:55
閱讀 2278·2019-08-29 13:21
閱讀 1330·2019-08-26 12:12
閱讀 2755·2019-08-26 10:37