Skip to content

Commit 2363457

Browse files
refactor 654
1 parent 0e0ab5e commit 2363457

File tree

1 file changed

+3
-27
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+3
-27
lines changed

src/main/java/com/fishercoder/solutions/_654.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,17 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5-
/**
6-
* 654. Maximum Binary Tree
7-
*
8-
* Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
9-
*
10-
* The root is the maximum number in the array.
11-
* The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
12-
* The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
13-
* Construct the maximum tree by the given array and output the root node of this tree.
14-
15-
Example 1:
16-
Input: [3,2,1,6,0,5]
17-
Output: return the tree root node representing the following tree:
18-
19-
6
20-
/ \
21-
3 5
22-
\ /
23-
2 0
24-
\
25-
1
26-
27-
Note:
28-
The size of the given array will be in the range [1,1000].
29-
*/
305
public class _654 {
316

327
public static class Solution1 {
338
/**
349
* Completely my original solution:
35-
*
10+
* <p>
3611
* As the problem states, I always broke the array into two halves and make notes
3712
* of current max node, then in the recursive call, we can recursively search
3813
* from its left part to construct its left subtree and its right part to construct its
39-
* right subtree.*/
14+
* right subtree.
15+
*/
4016
public TreeNode constructMaximumBinaryTree(int[] nums) {
4117
int max = Integer.MIN_VALUE;
4218
int maxIndex = -1;

0 commit comments

Comments
 (0)