forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_654.java
114 lines (104 loc) · 3.8 KB
/
_654.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
105
106
107
108
109
110
111
112
113
114
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
/**
* 654. Maximum Binary Tree
*
* Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
*
* The root is the maximum number in the array.
* The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
* The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
* Construct the maximum tree by the given array and output the root node of this tree.
Example 1:
Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree:
6
/ \
3 5
\ /
2 0
\
1
Note:
The size of the given array will be in the range [1,1000].
*/
public class _654 {
public static class Solution1 {
/**
* Completely my original solution:
*
* As the problem states, I always broke the array into two halves and make notes
* of current max node, then in the recursive call, we can recursively search
* from its left part to construct its left subtree and its right part to construct its
* right subtree.*/
public TreeNode constructMaximumBinaryTree(int[] nums) {
int max = Integer.MIN_VALUE;
int maxIndex = -1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
maxIndex = i;
}
}
TreeNode root = new TreeNode(max);
return constructMaxTree(root, maxIndex, nums, 0, nums.length - 1);
}
private TreeNode constructMaxTree(TreeNode root, int rootIndex, int[] nums, int start, int end) {
if (rootIndex > start) {
int max = Integer.MIN_VALUE;
int maxIndex = -1;
for (int i = start; i < rootIndex; i++) {
if (max < nums[i]) {
max = nums[i];
maxIndex = i;
}
}
root.left = constructMaxTree(new TreeNode(max), maxIndex, nums, start, rootIndex - 1);
}
if (rootIndex < end) {
int max = Integer.MIN_VALUE;
int maxIndex = -1;
for (int i = rootIndex + 1; i <= end; i++) {
if (max < nums[i]) {
max = nums[i];
maxIndex = i;
}
}
root.right = constructMaxTree(new TreeNode(max), maxIndex, nums, rootIndex + 1, end);
}
return root;
}
}
public static class Solution2 {
/**
* Completely my original solution as well, but more concise.
*/
public TreeNode constructMaximumBinaryTree(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
return construct(nums, 0, nums.length - 1);
}
TreeNode construct(int[] nums, int start, int end) {
if (start > end) {
return null;
}
int[] maxArray = findMax(nums, start, end);
TreeNode root = new TreeNode(maxArray[0]);
root.left = construct(nums, start, maxArray[1] - 1);
root.right = construct(nums, maxArray[1] + 1, end);
return root;
}
int[] findMax(int[] nums, int start, int end) {
int max = nums[start];
int maxIndex = start;
for (int i = start + 1; i <= end; i++) {
if (max < nums[i]) {
maxIndex = i;
max = nums[i];
}
}
return new int[]{max, maxIndex};
}
}
}