package leetcode;

/**
 * https://leetcode.com/problems/maximum-binary-tree/
 */
public class Problem654 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public TreeNode constructMaximumBinaryTree(int[] nums) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem654 prob = new Problem654();
        System.out.println(prob.constructMaximumBinaryTree(new int[]{3, 2, 1, 6, 0, 5}));
    }
}

@@ -337,35 +337,50 @@
     
-// TODO%0A        return null
+return construct(nums, 0, nums.length - 1)
 ;%0A  
@@ -389,29 +389,30 @@
 %7D%0A%0A    p
-ublic
+rivate
  static 
 void mai
@@ -407,167 +407,745 @@
 tic 
-void main(String%5B%5D args) %7B%0A        Problem654 prob = new Problem654();%0A        System.out.println(prob.constructMaximumBinaryTree(new int%5B%5D%7B3, 2, 1, 6, 0, 5%7D))
+TreeNode construct(int%5B%5D nums, int left, int right) %7B%0A        if (left %3E right) %7B%0A            return null;%0A        %7D%0A        int idx = max(nums, left, right);%0A        TreeNode node = new TreeNode(nums%5Bidx%5D);%0A        node.left = construct(nums, left, idx - 1);%0A        node.right = construct(nums, idx + 1, right);%0A        return node;%0A    %7D%0A%0A    private static int max(int%5B%5D nums, int left, int right) %7B%0A        int idx = left;%0A        int max = 0;%0A        for (int i = left; i %3C= right; i++) %7B%0A            if (i == left) %7B%0A                max = nums%5Bi%5D;%0A            %7D else %7B%0A                if (max %3C nums%5Bi%5D) %7B%0A                    max = nums%5Bi%5D;%0A                    idx = i;%0A                %7D%0A            %7D%0A        %7D%0A        return idx
 ;%0A  

Solve problem 654