-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximumBinaryTree2.java
More file actions
52 lines (37 loc) · 1.36 KB
/
Copy pathMaximumBinaryTree2.java
File metadata and controls
52 lines (37 loc) · 1.36 KB
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
// See: https://leetcode.com/problems/maximum-binary-tree-ii/
package leetcode.tree;
import java.util.ArrayList;
import java.util.List;
import leetcode.util.tree.TreeNode;
public class MaximumBinaryTree2 {
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
List<Integer> flattenList = new ArrayList<>();
dfs(root, flattenList);
flattenList.add(val);
return construct(flattenList, 0, flattenList.size() - 1);
}
private TreeNode construct(List<Integer> nums, int start, int end) {
if (start > end)
return null;
int maxIndex = findMaxIndex(nums, start, end);
TreeNode curr = new TreeNode(nums.get(maxIndex));
curr.left = construct(nums, start, maxIndex - 1);
curr.right = construct(nums, maxIndex + 1, end);
return curr;
}
private int findMaxIndex(List<Integer> nums, int start, int end) {
int maxIndex = start;
for (int i = start; i <= end; i++)
if (nums.get(i) > nums.get(maxIndex))
maxIndex = i;
return maxIndex;
}
private void dfs(TreeNode root, List<Integer> flattenTree) {
if (root == null) return;
dfs(root.left, flattenTree);
flattenTree.add(root.val);
dfs(root.right, flattenTree);
}
public static void main(String[] args) {
}
}