一:上码
二:上码
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public int getMaxIndex(int[] nums,int left,int right) {int index = 0;int maxx = -1;for (int i = left; i <= right; i++) {if (nums[i] > maxx) {maxx = nums[i];index = i;}} return index;}public TreeNode createTree(int[] nums,int left,int right) {if (left > right) return null;int index = getMaxIndex(nums,left,right);TreeNode node = new TreeNode(nums[index]);node.left = createTree(nums,left,index-1);node.right = createTree(nums,index+1,right);return node;}public TreeNode constructMaximumBinaryTree(int[] nums) {TreeNode ans = createTree(nums,0,nums.length-1);return ans;}
}