-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
654_Maximum_Binary_Tree.java
39 lines (39 loc) · 1.29 KB
/
654_Maximum_Binary_Tree.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
public class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return construct(nums, 0, nums.length);
}
public TreeNode construct(int[] nums, int l, int r) {
if (l == r)
return null;
int max_i = max(nums, l, r);
TreeNode root = new TreeNode(nums[max_i]);
root.left = construct(nums, l, max_i);
root.right = construct(nums, max_i + 1, r);
return root;
}
public int max(int[] nums, int l, int r) {
int max_i = l;
for (int i = l; i < r; i++) {
if (nums[max_i] < nums[i])
max_i = i;
}
return max_i;
}
/*public TreeNode constructMaximumBinaryTree(int[] nums) {
// https://leetcode.com/problems/maximum-binary-tree/discuss/106146/C++-O(N)-solution
Stack<TreeNode> stack = new Stack<>();
for (int i=0; i<nums.length(); i++) {
TreeNode curr = new TreeNode(nums[i]);
while (!stack.empty() && stack.peek().val < nums[i]) {
curr.left = stack.pop();
}
if (!stack.empty())
stack.peek().right = curr;
stack.push(curr);
}
while (stack.size() != 1) {
stack.pop()
}
return stack.peek();
}*/
}