递归
/** * 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 TreeNode constructMaximumBinaryTree(int[] nums) { return build(nums,0,nums.length-1); } public TreeNode build(int []nums, int lo,int hi){ if(lo>hi) return null; int index = -1; int Max = Integer.MIN_VALUE; for(int i=lo;i<=hi;i++){ if(nums[i]>Max){ index = i; Max = nums[i]; } } TreeNode root = new TreeNode(Max); root.left = build(nums,lo,index-1); root.right = build(nums,index+1,hi); return root; } }