Leetcode 654. Maximum Binary Tree

本文介绍了两种递归方法解决LeetCode问题,即通过数组拷贝和传数组索引构建最大二叉树。详细解析了代码实现、时间复杂度O(n)与空间复杂度O(n)的过程,并探讨了涉及的知识点,如递归、数组操作和数据结构。
摘要由CSDN通过智能技术生成

题目链接:https://leetcode.cn/problems/maximum-binary-tree/

方法一 递归 - 数组拷贝

1 方法思想

2 代码实现

/**
 * 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 == null || nums.length == 0) return null;
        
        int[] max = findMax(nums); // max[0] 代表当前数组中最大值,max[1]代表对应的下标
        TreeNode root = new TreeNode(max[0]);
        if (max[1] > 0){
            root.left = constructMaximumBinaryTree(Arrays.copyOfRange(nums, 0, max[1]));
        }
        if (nums.length > max[1] + 1){
            root.right = constructMaximumBinaryTree(Arrays.copyOfRange(nums, max[1] + 1, nums.length));
        }
        return root;
    }
    
    public int[] findMax(int[] nums) {
        int[] result = new int[2];
        int tempNum = Integer.MIN_VALUE;
        int tempIndex = -1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > tempNum){
                tempNum = nums[i];
                tempIndex = i;
            }
        }
        result[0] = tempNum;
        result[1] = tempIndex;
        return result;
    }
}

3 复杂度分析

时间复杂度:
空间复杂度:

4 涉及到知识点

5 总结

方法二 递归-传数组索引

1 方法思想

2 代码实现

/**
 * 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 == null || nums.length == 0) return null;
        return constructMaximum(nums, 0, nums.length);
    }
    
    public TreeNode constructMaximum(int[] nums, int startIndex, int endIndex) {
        if (endIndex <= startIndex) return null;

        int[] max = findMax(nums, startIndex, endIndex); // max[0] 代表当前数组中最大值,max[1]代表对应的下标
        TreeNode root = new TreeNode(max[0]);
        if (max[1] > startIndex){
            root.left = constructMaximum(nums, startIndex, max[1]);
        }
        if (endIndex > max[1] + 1){
            root.right = constructMaximum(nums, max[1] + 1, endIndex);
        }
        return root;
    }

    public int[] findMax(int[] nums, int startIndex, int endIndex) {
        int[] result = new int[2];
        int tempNum = Integer.MIN_VALUE;
        int tempIndex = -1;
        for (int i = startIndex; i < endIndex; i++) {
            if (nums[i] > tempNum){
                tempNum = nums[i];
                tempIndex = i;
            }
        }
        result[0] = tempNum;
        result[1] = tempIndex;
        return result;
    }

}

3 复杂度分析

时间复杂度:
空间复杂度:

4 涉及到知识点

5 总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值