File tree 1 file changed +3
-27
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +3
-27
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import com .fishercoder .common .classes .TreeNode ;
4
4
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
- */
30
5
public class _654 {
31
6
32
7
public static class Solution1 {
33
8
/**
34
9
* Completely my original solution:
35
- *
10
+ * <p>
36
11
* As the problem states, I always broke the array into two halves and make notes
37
12
* of current max node, then in the recursive call, we can recursively search
38
13
* from its left part to construct its left subtree and its right part to construct its
39
- * right subtree.*/
14
+ * right subtree.
15
+ */
40
16
public TreeNode constructMaximumBinaryTree (int [] nums ) {
41
17
int max = Integer .MIN_VALUE ;
42
18
int maxIndex = -1 ;
You can’t perform that action at this time.
0 commit comments