给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:
创建一个根节点,其值为 nums 中的最大值。
递归地在最大值 左边 的 子数组前缀上 构建左子树。
递归地在最大值 右边 的 子数组后缀上 构建右子树。
返回 nums 构建的 最大二叉树 。
/**
* 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) {
if(nums.length == 0){
return null;
}
return buildTree(nums,0,nums.length - 1);
}
public TreeNode buildTree(int[] nums,int left,int right){
if(left > right){
return null;
}
int index = -1;
int MaxVal = Integer.MIN_VALUE;
for(int i = left;i <= right;i++){
if(MaxVal < nums[i]){
index = i;
MaxVal = nums[i];
}
}
TreeNode root = new TreeNode(MaxVal);
root.left = buildTree(nums,left,index - 1);
root.right = buildTree(nums,index + 1,right);
return root;
}
}