当前位置:网站首页>【LeetCode每日一题】——654.最大二叉树
【LeetCode每日一题】——654.最大二叉树
2022-08-02 01:57:00 【IronmanJay】
一【题目类别】
- 二叉树
二【题目难度】
- 中等
三【题目编号】
- 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 的节点。
- 空数组,无子节点。
- [3,2,1] 中的最大值是 3 ,左边部分是 [] ,右边部分是 [2,1] 。
示例 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为数组元素个数
九【代码实现】
- 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;
}
}
- 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);
}
/*主函数省略*/
十【提交结果】
Java语言版

C语言版

边栏推荐
猜你喜欢

HSDC is related to Independent Spanning Tree

创新项目实战之智能跟随机器人原理与代码实现

云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣

Constructor instance method of typescript36-class

oracle查询扫描全表和走索引

【图像融合】基于加权和金字塔实现图像融合附matlab代码

大话西游创建角色失败解决

Understand the big model in seconds | 3 steps to get AI to write a summary

After graduating from three books, I was rejected by Tencent 14 times, and finally successfully joined Alibaba

Kubernetes之本地存储
随机推荐
浅谈国产ERP的“横纵竖”三向发展态势
数据链路层的数据传输
oracle查询扫描全表和走索引
ofstream,ifstream,fstream读写文件
Constructor instance method of typescript36-class
For effective automated testing, these software testing tools must be collected!!!
【轮式里程计】
kubernetes之服务发现
6-24漏洞利用-vnc密码破解
3个月测试员自述:4个影响我职业生涯的重要技能
Coding Experience Talk
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
喜报 | AR 开启纺织产业新模式,ALVA Systems 再获殊荣!
libcurl访问url保存为文件的简单示例
力扣 1161. 最大层内元素和
哈希表
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
Image fusion based on weighted 】 and pyramid image fusion with matlab code
手写一个博客平台~第三天
6-24 exploit-vnc password cracking