【LeetCode每日一题】——654.最大二叉树

一【题目类别】

  • 二叉树

二【题目难度】

  • 中等

三【题目编号】

  • 654.最大二叉树

四【题目描述】

  • 给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:
    • 创建一个根节点,其值为 nums 中的最大值。
    • 递归地在最大值 左边 的 子数组前缀上 构建左子树。
    • 递归地在最大值 右边 的 子数组后缀上 构建右子树。
  • 返回 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 中的所有整数 互不相同

七【解题思路】

  • 难点在于怎么在数组中找到合适的最大值,还要去根据这个最大值生成这棵二叉树
  • 在数组中找到最大值很简单,我们需要一个函数找到最大值索引
  • 找到最大值索引之后,根据此索引就能建立根节点了
  • 然后需要另外一个函数在数组中递归最大值索引的左边数组和右边数组,也就是左子树和右子树
  • 最后返回根节点即可

八【时间频度】

  • 时间复杂度: O ( n ) O(n) O(n),其中 n n n为数组元素个数
  • 空间复杂度: O ( l o g 2 N ) O(log_{2}N) O(log2N),其中 N N N为数组元素个数

九【代码实现】

  1. Java语言版
package Tree;

public class p654_MaximumBinaryTree {

    int val;
    p654_MaximumBinaryTree left;
    p654_MaximumBinaryTree right;

    public p654_MaximumBinaryTree(int val) {
        this.val = val;
    }

    public p654_MaximumBinaryTree(int val, p654_MaximumBinaryTree left, p654_MaximumBinaryTree right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public p654_MaximumBinaryTree() {
    }


    public static void main(String[] args) {
        int[] nums = {3, 2, 1, 6, 0, 5};
        p654_MaximumBinaryTree res = constructMaximumBinaryTree(nums);
        preOrder(res);
    }

    public static void preOrder(p654_MaximumBinaryTree root) {
        if (root != null) {
            System.out.print(root.val + " ");
            preOrder(root.left);
            preOrder(root.right);
        }
    }

    public static p654_MaximumBinaryTree constructMaximumBinaryTree(int[] nums) {
        return getMaxTree(nums, 0, nums.length - 1);
    }

    public static p654_MaximumBinaryTree getMaxTree(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        int maxIndex = getMaxIndex(nums, left, right);
        p654_MaximumBinaryTree root = new p654_MaximumBinaryTree(nums[maxIndex]);
        root.left = getMaxTree(nums, left, maxIndex - 1);
        root.right = getMaxTree(nums, maxIndex + 1, right);
        return root;
    }

    public static int getMaxIndex(int[] nums, int left, int right) {
        int max = -1;
        int maxIndex = left;
        for (int i = left; i <= right; i++) {
            if (max < nums[i]) {
                max = nums[i];
                maxIndex = i;
            }
        }
        return maxIndex;
    }

}
  1. C语言版
#include<stdio.h>
#include<stdlib.h>

struct TreeNode
{
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
};

int getMaxIndex(int* nums, int left, int right)
{
	int max = -1;
	int maxIndex = left;
	for (int i = left; i <= right; i++)
	{
		if (max < nums[i])
		{
			max = nums[i];
			maxIndex = i;
		}
	}
	return maxIndex;
}

struct TreeNode* getMaxTree(int* nums, int left, int right)
{
	if (left > right)
	{
		return NULL;
	}
	int maxIndex = getMaxIndex(nums, left, right);
	struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
	root->val = nums[maxIndex];
	root->left = getMaxTree(nums, left, maxIndex - 1);
	root->right = getMaxTree(nums, maxIndex + 1, right);
	return root;
}

struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize)
{
	return getMaxTree(nums, 0, numsSize - 1);
}

/*主函数省略*/

十【提交结果】

  1. Java语言版
    在这里插入图片描述

  2. C语言版
    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IronmanJay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值