[LeetCode] 654. Maximum Binary Tree

PX4 磁罗盘干扰分析 无人机上的电机电流会干扰无人机上搭载的磁罗盘,PX4官方提供了一些方式,通过分析飞行日志,判断电机是否对磁罗盘有严重影响。 阅读详情

Maximum Binary Tree

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:

  1. The root is the maximum number in the array.
  2. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
  3. The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.

Construct the maximum tree by the given array and output the root node of this tree.
在这里插入图片描述

解析

构建一个二叉树,树的根节点是当前列表中最大的值,分别递归构建走右子树,左子树的根节点类似于上面定义,是列表左边的最大值,同理,右子树的根节点是列表右边的最大值。

解法1:递归

找到最大值,建立根节点,再分别在列表左右两边递归建立左右子树。
借用辅助函数可以减少构建新列表的内存。

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        int size = nums.size();
        if(!size) return NULL;
        return helper(nums, 0, size-1);
    }
    
    TreeNode* helper(vector<int>& nums, int start, int end) {
        if(start > end) return NULL;
        int index = start;
        for(int i=start;i<=end;i++){
            if(nums[i] > nums[index]){
                index = i;
            }
        }
        TreeNode* root = new TreeNode(nums[index]);
        root->left = helper(nums, start, index-1);
        root->right = helper(nums, index+1, end);
        return root;
    }
};

不借用辅助函数,每次都要构建两个新的列表。

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        int size = nums.size();
        if(!size) return NULL;
        int index = 0;
        int m = INT_MIN;
        for(int i=0;i<size;i++){
            if(nums[i] > m){
                index = i;
                m = nums[i];
            }
        }        
        TreeNode* root = new TreeNode(m);
        vector<int> leftnode = vector<int>(nums.begin(), nums.begin()+index);
        vector<int> rightnode = vector<int>(nums.begin()+index+1, nums.end());
        root->left = constructMaximumBinaryTree(leftnode);
        root->right = constructMaximumBinaryTree(rightnode);
        return root;
    }
};

解法2:大神写法

大神们使用到了一个辅助数组res来让保持降序。遍历数组,对于每个遍历到的数字,创建一个结点,然后进行循环,如果数组res不空,且末尾结点值小于当前数字,那么将末尾结点连到当前结点的左子结点,并且移除数组中的末尾结点,这样可以保证子结点都会小于父结点。循环结束后,如果此时数组res仍不为空,说明结点值很大,那么将当前结点连到数组末尾结点的右子结点上。之后别忘了将当前结点加入数组res中,最后返回数组res的首结点即可。

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        vector<TreeNode*> res;
        for(int i=0;i<nums.size();i++)
        {
            TreeNode* node = new TreeNode(nums[i]);
            while(!res.empty() && res.back()->val < nums[i])
            {
                node->left = res.back();
                res.pop_back();
            }
            if(!res.empty())
                res.back()->right = node;
            res.push_back(node);
        }
        return res[0];
    }
};

参考

http://www.cnblogs.com/grandyang/p/7513099.html

【Dify】PPT生成工具应用 本文介绍了一款自动化PPT生成工具,通过智能接口将文本内容高效转化为结构化演示文档。该系统采用三阶段工作流(内容输入、HTTP请求上传、文件输出),无需复杂操作即可生成专业PPT。工具适用于教学课件、产品发布、企业培训等场景,能显著提升内容表达效率,特别适合初学者快速制作演示材料。文章详细说明了核心节点、应用场景及开发资源,为文本到PPT的自动化转换提供了实用解决方案。 阅读详情

相关推荐

单片机笔记十二:华大HC32L136超低功耗设计实战

本文详细介绍了华大HC32L136单片机的超低功耗设计实战,包括深度睡眠模式下仅0.5μA的电流消耗、五种功耗模式的对比与配置、外设使用技巧及硬件级优化方法。通过实际项目案例,展示了如何利用ARM Cortex-M0+内核的HC32L136显著延长电池寿命,适用于智能门锁、额温枪等低功耗设备。

weixin_29189373的博客 394

leetcode 654. Maximum Binary Tree(构建最大二叉树)

题目要求 给定一个没有重复的整数数组。 此阵列上的最大树构建定义如下: 1、根是数组中的最大数字。 2、左子树是根的左部分子数组构造的最大树。 3、右子树是根的右部分子数组构造的最大树。 4、通过给定数组构造最大树并输出此树的根节点。 输入实例 Example 1: Input: [3,2,1,6,0,5] Output: return the tree root node representi...

爱喝舒化奶的博客 467

从零到一:YOLOv11高帧率模型在RDK X5上的ONNX转换与部署实战

本文详细介绍了将最新的YOLOv11高帧率模型部署到地平线RDK X5开发板的完整实战流程。内容涵盖从PyTorch模型导出ONNX、地平线工具链环境配置与模型量化编译,到编写高效的板端推理代码,并针对高帧率目标进行了后处理优化。通过实战避坑指南,帮助开发者在RDK X5上实现30FPS以上的实时目标检测。

weixin_29089057的博客 139

leetcode 654. Maximum Binary Tree

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number in the array.The left subtree is the maximum tree constructed f

淡然坊 1442

编译CycloneDDS和CycloneDDS-CXX踩过的坑——CMake Error at CMakeLists.txt:227 (find_package):

最近编译CycloneDDS和CycloneDDS-CXX踩了两个坑,花了不少时间。做个简单的记录,免得以后又踩到。注意:CycloneDDS-CXX编译依赖CycloneDDS,因此必须先编译CycloneDDS需要严格按照步骤进行,否则要入坑步骤如下,1先编译CycloneDDS以上步骤完成CycloneDDS的编译和安装,生成的bin include lib等位于install目录下面。

usstmiracle的博客 1900

Python实战:3步搞定DICOM转JPG/PNG(附批量处理脚本)

本文详细介绍了如何使用Python在3步内将DICOM医学影像转换为JPG/PNG格式,并提供了批量处理脚本。通过Pydicom、Pillow和NumPy库的组合,解决了像素值处理、批量转换效率和医学图像特性保留等关键问题,适合医疗AI开发者和放射科医生快速集成到工作流中。

numpy6sculptor的博客 592

LeetCode //C - 654. Maximum Binary Tree

【代码】LeetCode //C - 654. Maximum Binary Tree。

Made in Code 2016

保姆级教程:用CANoe手把手复现ECU刷写全流程(含BootLoader三阶段详解)

本文提供了一份详细的CANoe ECU刷写实战指南,涵盖BootLoader三阶段的深度解析。从实验环境搭建到预编程、主编程及后编程阶段,逐步讲解ECU刷写的关键步骤和隐藏逻辑,特别强调UDS诊断和CAN通信的精确控制,帮助开发者掌握ECU刷写的核心技术。

weixin_29234423的博客 529

leetcode 654 Maximum Binary Tree

leetcode 654 Maximum Binary Tree 题意:按给定的数组建树,数组数值无重复。 1.The root is the maximum number in the array. 1.根是数组最大的元素。 2.The left subtree is the maximum tree constructed from left part subarray divided by ...

UPUP&TOPTOP 160

嵌入式工程师成长之路(7)——算法学习

算法大全1、风仲达——算法笔记2、坐在马桶上学算法3、啊哈算法4、深度强化学习实验室。

云轩阁 4万+

前端开发 - 完美解决video.js和flv.js视频同时播放数量限制的问题,即浏览器针对同一域名的请求限制问题(解决flv.js/video.js在页面播放最多不超过6个问题、浏览器并发请求限制数

flv.js,video.js,vue,reactive,uniapp,前端开发,flv视频同时播放有限制怎么办,浏览器限制多个视频同时播放怎么办,使用flv.js在前端播放,最多只能播放6个视频,突破浏览器域名并发限制,如何解决flvjs与videojs视频播放同时播放数量限制,video.js 一个页面同时播放多个视频的实例代码,解决网页视频最大只能播放6路的问题,浏览器只能播放6路http-flv流的解决方法,一个浏览器播放5个或者6个flv的视频就不能播放了,video.js 无法在页面同时播放多个

✅ 精选推荐文章 4995

Leetcode654. Maximum Binary Tree

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:The root is the maximum number in the array. The left subtree is the maximum tree constructed from

林剑的博客 1165

【车联网】Automotive 软件过程能力成熟度模型——ASPICE标准入门介绍

ASPICE标准详细定义了与汽车电子软件开发相关的各种术语和概念,包括软件组件、系统集成、测试和验证等。这些定义确保了所有开发团队在使用相同术语时具有一致的理解。

奶爸的编程之路,也就一周冷个三天 2678

leetcode--654. Maximum Binary Tree

参考:https://leetcode.com/problems/maximum-binary-tree/discuss/106147/C%2B%2B-8-lines-O(n-log-n)-map-plus-stack-with-binary-search Given an integer array with no duplicates. A maximum tree building on ...

丫丫的博客 378

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Leetcode 654 Maximum Binary Tree

题目链接: 654. Maximum Binary Tree 题目描述 Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number in the a...

Mereder的博客 116

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

【Leetcode】654. Maximum Binary Tree

2、左子树由最大数字左边的数构成,并且左子树也是个最大树;3、右子树由最大数字右边的数构成,并且右子树也是个最大树;本质上是构建一个笛卡儿树,需要用单调栈存树的右链。1、root是数组中的最大数字;

数学、算法爱好者的博客 166

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

LeetCode-654. Maximum Binary Tree

最近在leetcode,觉得不刷题的话,思维能力会下降。 题目: Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number in the array. The left subtre...

Hungxum的专栏 287

[leetcode]654. Maximum Binary Tree(Python)

[leetcode]654. Maximum Binary Tree题目描述题目理解解题思路MyMethodTime 题目描述 Category:Medium Tree Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: 1.The root ...

La_Ragazza的博客 279

[LeetCode]654. Maximum Binary Tree

[LeetCode]654. Maximum Binary Tree题目描述思路参考了答案写法,用了栈 若栈不为空,若栈顶元素小于当前值,贼一直更新当前值的左子节点,直至栈空或者栈顶元素不小于当前值 之后,若栈不为空,栈顶元素右子节点为当前节点 当前节点入栈 知道遍历数组结束代码#include <iostream> #include <vector>using namespace std;

Lcharon的博客 512

leetcode654: Maximum Binary Tree

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number in the array.The left subtree is the maximum tree constructed f

sinat_35425429的博客 349

[leetcode]654. Maximum Binary Tree

[leetcode]654. Maximum Binary Tree Analysis 周二快乐~—— [ummm~] Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum nu...

weixin_32135877的博客 206

SPI数字隔离实战:MAX14431BAEE+与STM32L152ZD的工业链路设计

在工业现场,SPI通信常因两地电位差和地环路电流而出现数据错乱、设备死机,甚至IO口损坏。数字隔离器通过电容或磁耦合实现信号与地回路的双重隔离,相比光耦具备更低传播延迟和更高通道一致性,是高速同步协议可靠传输的关键。以多通道隔离器MAX14431BAEE+搭配低功耗Cortex-M3芯片STM32L152ZD为例,文章详细拆解隔离电源设计、通道方向映射、PCB隔离带布局、固件时序余量计算与调试方法,帮助工程师避开光耦衰减、底层铜箔跨隔离带、DMA与CS配合等典型陷阱,为工业板卡的隔离SPI链路提供一套可直接

weixin_34326429的博客 408

LeetCode654. Maximum Binary Tree

本题思路类似105和106,都利用了切分数组+构建树的思路,本题只有一个数组,因此相比105和106题更简单。

叶卡捷琳堡的博客 310

【LeetCode】654. Maximum Binary Tree

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number in the array.The left subtree is the maximum tree constructed f

lx583274568的专栏 392

leetcode 654. Maximum Binary Tree(python)

描述 You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm: Create a root node whose value is the maximum value in nums. Recursively build the left subtree on the subar

王大呀呀的博客 292
上一篇: [LeetCode] 652. Find Duplicate Subtrees
下一篇: [LeetCode] 655. Print Binary Tree
Peng_maple
博客等级 码龄10年 0粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值