-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumBinaryTree.java
More file actions
51 lines (44 loc) · 1.36 KB
/
MaximumBinaryTree.java
File metadata and controls
51 lines (44 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
package com.leetcode.problems.medium;
/**
* @author neeraj on 05/10/19
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class MaximumBinaryTree {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public static void main(String[] args) {
constructMaximumBinaryTree(new int[]{3, 2, 1, 6, 0, 5});
}
public static TreeNode constructMaximumBinaryTree(int[] nums) {
TreeNode root = maxBinaryTreeUtil(nums, 0, nums.length - 1);
System.out.println(root);
return root;
}
private static TreeNode maxBinaryTreeUtil(int[] nums, int low, int high) {
if (low > high) return null;
TreeNode root;
int maxValIndex = getMaxValIndex(nums, low, high);
root = new TreeNode(nums[maxValIndex]);
root.left = maxBinaryTreeUtil(nums, low, maxValIndex - 1);
root.right = maxBinaryTreeUtil(nums, maxValIndex + 1, high);
return root;
}
private static int getMaxValIndex(int[] nums, int low, int high) {
int max = Integer.MIN_VALUE;
int maxValIndex = 0;
for (int i = low; i <= high; i++) {
if (max < nums[i]) {
max = nums[i];
maxValIndex = i;
}
}
return maxValIndex;
}
}