给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:
创建一个根节点,其值为 nums 中的最大值。
 递归地在最大值 左边 的 子数组前缀上 构建左子树。
 递归地在最大值 右边 的 子数组后缀上 构建右子树。
 返回 nums 构建的 最大二叉树 。
示例 1:

图1 最大二叉树
输入:nums = [3,2,1,6,0,5]
 输出:[6,3,5,null,2,0,null,null,1]
来源:力扣(LeetCode)
 链接:https://leetcode.cn/problems/maximum-binary-tree
 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解:从数组中找到最大元素的索引,不重建数组,用方法的两个参数定义左右边界。建立节点,节点左子节点,当前左边界,最大元素索引-1;节点右节点,最大元素索引+1, 当前右边界。左大于右,直接返回空。
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return build(nums, 0, nums.length - 1);
    }
    public TreeNode build(int[] nums, int left, int right) {
        if (left > right) return null;
        int max = getMax(nums, left, right);
        TreeNode node = new TreeNode(nums[max]);
        node.left = build(nums, left, max - 1);
        node.right =  build(nums, max + 1, right);
        return node;
    }
    public int getMax(int[] nums, int left, int right) {
        int max = left;
        for (int i = left + 1; i <= right; i++) {
            max = nums[max] > nums[i] ? max : i;
        }
        return max;
    }
}


















