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
buildmethod. - 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]
1 /** 2 * Definition of SegmentTreeNode: 3 * public class SegmentTreeNode { 4 * public int start, end; 5 * public SegmentTreeNode left, right; 6 * public SegmentTreeNode(int start, int end) { 7 * this.start = start, this.end = end; 8 * this.left = this.right = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 /** 14 *@param start, end: Denote an segment / interval 15 *@return: The root of Segment Tree 16 */ 17 public SegmentTreeNode build(int start, int end) { 18 if (start > end ) return null; 19 20 if (start == end) { 21 return new SegmentTreeNode(start, start); 22 } else { 23 SegmentTreeNode root = new SegmentTreeNode(start, end); 24 root.left = build(start, (start + end) / 2); 25 root.right = build((start + end) / 2 + 1, end); 26 return root; 27 } 28 } 29 }
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
buildmethod. - 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)
1 /** 2 * Definition of SegmentTreeNode: 3 * public class SegmentTreeNode { 4 * public int start, end, max; 5 * public SegmentTreeNode left, right; 6 * public SegmentTreeNode(int start, int end, int max) { 7 * this.start = start; 8 * this.end = end; 9 * this.max = max 10 * this.left = this.right = null; 11 * } 12 * } 13 */ 14 public class Solution { 15 16 public SegmentTreeNode build(int[] A) { 17 if (A == null || A.length == 0) return null; 18 19 return build(A, 0, A.length - 1); 20 } 21 22 public SegmentTreeNode build(int[] A, int start, int end) { 23 if (start > end) return null; 24 25 if (start == end) { 26 return new SegmentTreeNode(start, end, A[start]); 27 } else { 28 SegmentTreeNode root = new SegmentTreeNode(start, end, 0); 29 root.left = build(A, start, (start + end) / 2); 30 root.right = build(A, (start + end) / 2 + 1, end); 31 root.max = Math.max(root.left.max, root.right.max); 32 return root; 33 } 34 } 35 }

浙公网安备 33010602011771号