【陪伴式刷题】Day 18|二叉树|654.最大二叉树(Maximum Binary Tree)

刷题顺序按照代码随想录建议

题目描述

英文版描述

You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:

  1. Create a root node whose value is the maximum value in nums.
  2. Recursively build the left subtree on the subarray prefix to the left of the maximum value.
  3. Recursively build the right subtree on the subarray suffix to the right of the maximum value.

Return the maximum binary tree built from nums.

Example 1:

Input: nums = 3,2,1,6,0,5 Output: 6,3,5,null,2,0,null,null,1 Explanation: The recursive calls are as follow: - The largest value in 3,2,1,6,0,5 is 6. Left prefix is 3,2,1 and right suffix is 0,5. - The largest value in 3,2,1 is 3. Left prefix is \[\] and right suffix is 2,1. - Empty array, so no child. - The largest value in 2,1 is 2. Left prefix is \[\] and right suffix is 1. - Empty array, so no child. - Only one element, so child is a node with value 1. - The largest value in 0,5 is 5. Left prefix is 0 and right suffix is \[\]. - Only one element, so child is a node with value 0. - Empty array, so no child.

Example 2:

Input: nums = 3,2,1 Output: 3,null,2,null,1

Constraints:

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 1000
  • All integers in nums are unique.

英文版地址

leetcode.com/problems/ma...

中文版描述

给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:

  1. 创建一个根节点,其值为 nums 中的最大值。
  2. 递归地在最大值 左边 的 子数组前缀上 构建左子树。
  3. 递归地在最大值 右边 的 子数组后缀上 构建右子树。

返回 nums 构建的 最大二叉树 ****。

示例 1:

输入: nums = 3,2,1,6,0,5 输出: 6,3,5,null,2,0,null,null,1 解释: 递归调用如下所示: - 3,2,1,6,0,5 中的最大值是 6 ,左边部分是 3,2,1 ,右边部分是 0,5 。 - 3,2,1 中的最大值是 3 ,左边部分是 \[\] ,右边部分是 2,1 。 - 空数组,无子节点。 - 2,1 中的最大值是 2 ,左边部分是 \[\] ,右边部分是 1 。 - 空数组,无子节点。 - 只有一个元素,所以子节点是一个值为 1 的节点。 - 0,5 中的最大值是 5 ,左边部分是 0 ,右边部分是 \[\] 。 - 只有一个元素,所以子节点是一个值为 0 的节点。 - 空数组,无子节点。

示例 2:

输入: nums = 3,2,1 输出: 3,null,2,null,1

提示:

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 1000
  • nums 中的所有整数 互不相同

中文版地址

leetcode.cn/problems/ma...

解题方法

ini 复制代码
/**
 * 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 recursion(nums, 0, nums.length);
    }

    // 左闭右开
    private TreeNode recursion(int[] nums, int start, int end) {
        if (end <= start) {
            return null;
        }
        if (end - start == 1) {
            return new TreeNode(nums[start]);
        }
        //  获取根节点序号
        int max = nums[start];
        int maxIndex = start;
        for (int i = start; i < end; i++) {
            if (nums[i] > max) {
                max = nums[i];
                maxIndex = i;
            }
        }
        TreeNode root = new TreeNode(nums[maxIndex]);
        if (maxIndex - start > 0) {
            root.left = recursion(nums, start, maxIndex);
        }
        if (end - (maxIndex + 1) > 0) {
            root.right = recursion(nums, maxIndex + 1, end);
        }
        return root;
    }
        
}

复杂度分析

  • 时间复杂度:O(n^2),其中 n 是数组 nums 的长度,在最坏的情况下,数组严格递增或递减,需要递归 nnn 层,第 i (0≤i<n) 层需要遍历 n−i 个元素以找出最大值,总时间复杂度为 O(n^2)
  • 空间复杂度:O(n),为递归过程中栈的开销,最坏情况下为 O(n)
相关推荐
独泪了无痕5 小时前
探索Pinyin4j-Java中的拼音转换利器
java·后端
道尔柯南6 小时前
C++ string 类详解:从常用接口到深拷贝,彻底掌握字符串处理
android·java·c++
kingx37 小时前
Bugku 3122 谷歌小恐龙题解:纯离线逆向 Vault 的 AES-GCM 加密资源(附 Python 解密器)
java·逆向·ctf·bugku·aes-gcm
Dream it possible!9 小时前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划
DevOpenClub10 小时前
职教高考控制线 API:招生类别枚举、筛选与空分数含义
java·数据库·高考
心抵鹊10 小时前
算法之双指针系列(一)
算法·leetcode
露天赏雪11 小时前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
天天被压力11 小时前
【A股分钟级高频数据完全手册 #06】北交所分钟级数据接口:京市股票Pro版最新分时
java·人工智能·python
天天被压力11 小时前
【A股分钟级高频数据完全手册 #05】指数历史分钟数据接口:Pro版沪深指数分时回放
java·人工智能·python
captain37611 小时前
▲统一功能——DispatcherServlet源码分析
java·spring·java-ee·log4j·maven