LeetCode Binary Tree Maximum Path Sum

【LeetCode】Binary Tree Maximum Path Sum 解题报告 【题目】 Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \ 2 3 Re 阅读详情

原题链接在这里:https://leetcode.com/problems/binary-tree-maximum-path-sum/

maxVal中包含着最后要返回的结果。

helper函数是求当前root往下的最大路径,先算左边路径,返回值直接和0比较,若不是正数就返回0,然后同样方法算右边路径。

当前最深路径看当前root的val加上左边或者右边,哪个大,helper函数返回大的那个。

同时更新maxVal, 看看当前root的val同时加上左边和右边有没有比现有的maxVal包含值大。

AC Java:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxPathSum(TreeNode root) {
        int [] maxValue = new int[1];
        maxValue[0] = Integer.MIN_VALUE;
        helper(root,maxValue);
        return maxValue[0];
    }
    
    //此函数返回root往下最大路径
    private int helper(TreeNode root, int [] maxVal){
        if(root == null){
            return 0;
        }
        //leftMax, rightMax could be 0 or positive
        int leftMax = Math.max(0,helper(root.left, maxVal));
        int rightMax = Math.max(0,helper(root.right,maxVal));
        //当前root往下的最大路径
        int curMaxDeepth = Math.max(root.val + leftMax, root.val + rightMax);
        //Update maxVal
        maxVal[0] = Math.max(maxVal[0], leftMax+root.val+rightMax);
        return curMaxDeepth;
    }
}


STM32固定翼增稳飞控设计:姿态解算、双环PID与一键救机实现 姿态稳定控制是无人机与航模飞控系统的核心基础,其本质是通过传感器融合(如IMU)实时感知飞行器姿态,并基于控制理论(如PID)生成修正指令以抑制扰动。该技术广泛应用于教育航模、轻型固定翼平台及嵌入式飞控开发场景,具有低延迟、高确定性、资源敏感等典型工程特征。本文围绕STM32F401与MPU6050构建的硬件闭环系统,深入解析互补滤波姿态解算原理与俯仰/横滚双环PID控制结构,同时聚焦‘一键救机’这一面向新手的关键安全机制——通过分阶段角速率抑制与平滑角度拉平策略,在80ms内启动响应,显著提升飞行容错能力 阅读详情

相关推荐

【记录】游戏体验报告怎么写(自我成长篇)

2019年3月更新: 这个倒是不用推翻,我个人虽然写的东西还是有一些偏差的,但是“表哥”说的倒是没毛病的,这篇主要是为了记录用,具体的干货也有,都在图片里,请自行提取 =====18年初原答案 作为一个游戏策划新人,游戏分析可能抓不到点子上,试着总结记录一下游戏体验报告分析的成长(因为水平不同,视野可能也不一样,也许之后几年回头看这些东西都是小儿科或者是错误的,所以只写出当下的想...

eat_human的博客 6万+

leetcode之binary-tree-maximum-path-sum

leetcode之binary-tree-maximum-path-sum 题目 Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, return 6; 题意 给出一...

此心光明,亦复何言~ 263

WPF 解决listview值更改时页面没有刷新

WPF 解决listview值更改时页面没有刷新。

weixin_52475977的博客 1385

[LeetCode] Binary Tree Maximum Path Sum

[Problem] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree,   1 / \2  3 Return 6. [Analysis] 第一步: 在每个结点i

1.02^365的成长裂变 3666

Claude Desktop Cowork国内实操指南:本地文件自动化办公

大语言模型正从‘问答式AI’迈向‘执行型协作者’,其核心能力在于直接操作本地文件、调用系统工具并嵌入真实工作流。Claude Desktop的Cowork模式正是这一范式的代表,它通过轻量沙箱VM实现对Excel、PPT、PDF、音视频等办公资产的读写与处理,无需手动编码即可完成数据清洗、会议纪要生成、自动化报告输出等任务。技术价值体现在权限可控、环境隔离、结果可交付;典型场景覆盖行政文秘的文档流转、销售运营的数据分析、开发者的脚本调试与环境验证。本文聚焦国内用户高频卡点——网络链路、系统权限与工作流断层,

weixin_30617737的博客 465

【OpenClaw从入门到精通】第05篇:实战!OpenClaw自动化搞定邮件/日历/文件管理(2026实测避坑版)

本文聚焦OpenClaw核心实用场景,手把手教你实现邮件、日历、文件管理的全自动化。通过安装三大核心技能,配置IMAP/SMTP邮箱、跨平台日历及文件沙盒权限,结合4个虚拟实战案例,演示从邮件自动摘要、会议冲突预警到下载文件夹分类的完整流程。文中包含2026实测的命令代码、配置步骤、排障方案,所有案例需在测试环境验证,代码未上传GitHub。兼顾新手入门与进阶需求,让OpenClaw从“能聊天”升级为“能干活”,帮你节省80%重复工作时间。

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 2674

LeetCode力扣之124. Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path...

LeeWei 228

如何在多 GPU 配置上运行 Unsloth:数据并行或模型并行

当工作负载受限于内存(模型或批次无法放入单张显卡)、或受限于计算且计算与通信比率较高时(即每块 GPU 的额外计算量超过同步参数的开销,如在 DDP 中,或超过传递激活值的开销,如在模型并行中),多 GPU 配置才开始显现优势。在松散连接的显卡上(尤其是 PCIe 版本的 H100 或 A100),额外的设备间数据传输会导致性能显著低于单 GPU 运行。让我们厘清概念,明确说明如何通过模型并行(将模型拆分到多个 GPU)或数据并行(在每个 GPU 上复制模型)来跨多 GPU 运行 Unsloth。

人工智能领域专家 645

LeetCode(124) Binary Tree Maximum Path Sum

题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connectio

指尖飞舞 4138

【Bluetooth蓝牙开发】十一、一文秒懂 | 超详细的Bluez交叉编译

1、开篇词 2、蓝牙开发入门 3、蓝牙协议栈总览 4、蓝牙协议栈——物理层 5、蓝牙协议栈——链路层 6、蓝牙协议栈——传输层 7、蓝牙协议栈——L2CAP协议 8、蓝牙协议栈——ATT协议 9、蓝牙协议栈——GATT协议 10、蓝牙通信流程分析 11、蓝牙协议栈——Bluez交叉编译 12、蓝牙调试工具汇总

Donge's Blog 3975

从传感器到执行-智能家居 Agent 的感知-决策-执行闭环

本文提出了一种面向智能家居的感知型Agent架构,通过"感知-决策-执行"的闭环系统实现智能化控制。感知模块整合多源传感器数据,决策模块基于规则或机器学习模型生成控制策略,执行模块完成设备操作。系统能够实时监测环境参数和用户状态,自动调节温度、照明等设备,提升家居舒适度。该架构可扩展为多模态感知与强化学习相结合的智能系统,为智慧家居发展提供技术基础。

一键难忘的博客 1119

Leetcode 124 Binary Tree Maximum Path Sum

**Leetcode 124 Binary Tree Maximum Path Sum 题目描述 Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any n...

qq_43271202的博客 210

重回童年的经典系列☀️|【炸弹人小游戏】制作过程+解析 | 收藏起来跟曾经的小伙伴一起梦回童年!

Unity,小游戏开发。 本篇文章对童年经典小游戏——炸弹超人进行制作和解析。不知道有没有勾起你童年的回忆呢!快来和我一起制作这个童年经典的小游戏吧! 敬请品尝!

努力前行,总会成为自己心中的那道光 13万+

Leetcode 每日一题:Binary Tree Maximum Path Sum

Google 高频面试题:Leetcode 124: Binary Tree Max Sum 题目详解

2401_85421495的博客 1093

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

[leetcode]Binary Tree Maximum Path Sum

近期为了准备笔试开始刷leetcode,记录一下希望自己可以坚持! Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:

iam_lain的博客 591

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

【LeetCode with Python】 Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \ 2 3 Return 6.

XNERV SURVEYS 1882

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

LeetCode——Binary Tree Maximum Path Sum

链接:http://leetcode.com/onlinejudge#question_124 原题: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary t

niaokedaoren的专栏 1413

Leetcode Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The

每天都要有进步 303

[LeetCode 124] Binary Tree Maximum Path Sum

题目链接:binary-tree-maximum-path-sum /** * * Given a binary tree, find the maximum path sum. * * The path may start and end at any node in the tree. * * For example: Given the below binary

瓦尔登湖 731

[leetcode] 124. Binary Tree Maximum Path Sum

题目: Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example: Given the below binary tree, 1 / \ 2 3 Return 6. 题意: 给定一棵二叉树,找出从任意一个起点,到达任意

u014673347的专栏 917

【Leetcode】Binary Tree Maximum Path Sum

题目链接:https://leetcode.com/problems/binary-tree-maximum-path-sum/题目: Given a binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node

业精于勤,荒于嬉 1898

LeetCode124:Binary Tree Maximum Path Sum

Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connectio...

卷卷萌的博客 575

LeetCode 124. Binary Tree Maximum Path Sum 题解

本题是二叉树的经典问题,主要考察对二叉树遍历和递归的理解和应用。通过使用深度优先搜索,我们可以高效地计算二叉树的最大路径和。深度优先搜索的核心思想是:对于每个节点,计算以该节点为根的子树的最大贡献值,同时计算经过该节点的最大路径和,并更新全局最大路径和。这种方法不仅适用于二叉树的最大路径和问题,还可以应用于许多其他需要遍历二叉树并计算路径和的场景。掌握深度优先搜索的思想,对于解决这类问题非常重要。

cannonjinx的博客 4433

Leetcode之Binary Tree Maximum Path Sum

题目: Given anon-emptybinary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child conn...

qq_35455503的博客 220

LeetCode_Binary Tree Maximum Path Sum

LeetCode_Binary Tree Maximum Path Sum 解题思路

小胜爱捣鼓 1093
上一篇: LeetCode Binary Search Tree Iterator
下一篇: LeetCode Climbing Stairs
Dylan_Java_NYC
博客等级 码龄11年 1粉丝 134原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值