-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path654.MaximumBinaryTree.java
More file actions
36 lines (29 loc) · 1021 Bytes
/
654.MaximumBinaryTree.java
File metadata and controls
36 lines (29 loc) · 1021 Bytes
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
/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left
* = left; this.right = right; } }
*/
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return build(nums, 0, nums.length - 1);
}
public TreeNode build(int[] arr, int start, int end) {
if (start > end)
return null;
int index = -1;
int maxVal = Integer.MIN_VALUE;
// 因为写成 i < end,排查20分钟。
for (int i = start; i <= end; i++) {
int itemValue = arr[i];
if (itemValue > maxVal) {
maxVal = itemValue;
index = i;
}
}
TreeNode root = new TreeNode(maxVal);
root.left = build(arr, start, index - 1);
root.right = build(arr, index + 1, end);
return root;
}
}