-
Notifications
You must be signed in to change notification settings - Fork 14
/
Solution654.java
104 lines (81 loc) · 2.75 KB
/
Solution654.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package leetcode.segmenttree;
import leetcode.TreeNode;
public class Solution654 {
public TreeNode constructMaximumBinaryTree(int[] nums) {
SegmentTree segmentTree = new SegmentTree(nums);
return recursion(1, nums.length, segmentTree);
}
private TreeNode recursion(int left, int right, SegmentTree segmentTree) {
// 递归结束条件
if (left > right) {
return null;
}
SegmentTree.Node node = segmentTree.query(1, left, right);
TreeNode treeNode = new TreeNode(node.val);
treeNode.left = recursion(left, node.index - 1, segmentTree);
treeNode.right = recursion(node.index + 1, right, segmentTree);
return treeNode;
}
static class SegmentTree {
static class Node {
int val;
int index;
int left;
int right;
public Node(int left, int right) {
this.left = left;
this.right = right;
}
}
private int[] nums;
private Node[] tree;
public SegmentTree(int[] nums) {
this.nums = nums;
tree = new Node[nums.length * 4];
// 构建树
build(1, 1, nums.length);
}
private void build(int pos, int left, int right) {
tree[pos] = new Node(left, right);
if (left == right) {
tree[pos].val = nums[left - 1];
// 维护当前节点的索引值
tree[pos].index = left;
return;
}
int mid = left + right >> 1;
build(pos << 1, left, mid);
build(pos << 1 | 1, mid + 1, right);
pushUp(pos);
}
private void pushUp(int pos) {
Node max;
if (tree[pos << 1].val < tree[pos << 1 | 1].val) {
max = tree[pos << 1 | 1];
} else {
max = tree[pos << 1];
}
tree[pos].val = max.val;
tree[pos].index = max.index;
}
public Node query(int pos, int left, int right) {
if (tree[pos].left == left && tree[pos].right == right) {
return tree[pos];
}
int mid = tree[pos].left + tree[pos].right >> 1;
if (right <= mid) {
return query(pos << 1, left, right);
} else if (left > mid) {
return query(pos << 1 | 1, left, right);
} else {
Node x = query(pos << 1, left, mid);
Node y = query(pos << 1 | 1, mid + 1, right);
if (x.val > y.val) {
return x;
} else {
return y;
}
}
}
}
}