[LeetCode]654. Maximum Binary Tree最大堆二叉树

Posted stAr_1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode]654. Maximum Binary Tree最大堆二叉树相关的知识,希望对你有一定的参考价值。

每次找到数组中的最大值,然后递归的构建左右树

public TreeNode constructMaximumBinaryTree(int[] nums) {
        if (nums.length==0) return null;
        return builder(nums,0,nums.length-1);
    }
    public TreeNode builder(int[] nums,int sta,int end)
    {
        /*
        思路就是每次找到最大值,然后分为两个子数组递归构建左右树
         */
        if (sta>end) return null;
        int max = Integer.MIN_VALUE;
        int index = -1;
        for (int i = sta; i <= end ; i++) {
            if (nums[i]>max)
            {
                max = nums[i];
                index = i;
            }
        }
        TreeNode root = new TreeNode(max);
        root.left = builder(nums,sta,index-1);
        root.right = builder(nums,index+1,end);
        return root;
    }

 

以上是关于[LeetCode]654. Maximum Binary Tree最大堆二叉树的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode - 654. Maximum Binary Tree

[LeetCode] 654. Maximum Binary Tree

[LeetCode] 654. Maximum Binary Tree

[LeetCode] 654. Maximum Binary Tree 最大二叉树

[Leetcode]654.Maximum Binary Tree

[LeetCode]654. Maximum Binary Tree最大堆二叉树