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 阅读详情

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.


Show Tags
Have you met this question in a real interview?

首先需要搞清楚一个子问题:给你一棵树,求从根到叶子的最大节点和,怎么求?

so easy,递归:

int f(TreeNode *root){
        if(root==NULL)
            return 0;
        int l=f(root->left);
        int r=f(root->right);
        return max(max(l+root->val,r+root->val),root->val);
    }

那么,在递归中求各个子树的经过根节点的路径和的最大值,即可。


  1. int maxnum=INT_MIN;
  2.     int f(TreeNode *root){
  3.         if(root==NULL)
  4.             return 0;
  5.         int l=f(root->left);
  6.         int r=f(root->right);
  7.         maxnum=max(max(l,0)+max(r,0)+root->val,maxnum);
  8.         return max(max(l+root->val,r+root->val),root->val);
  9.     }
  10.     int maxPathSum(TreeNode *root) {
  11.         if(root==NULL)
  12.             return 0;
  13.         f(root);
  14.         return maxnum;
  15.     }


<script>window._bd_share_config={"common":{"bdsnskey":{},"bdtext":"","bdmini":"2","bdminilist":false,"bdpic":"","bdstyle":"0","bdsize":"16"},"share":{}};with(document)0[(getelementsbytagname('head')[0]||body).appendchild(createelement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new date()/36e5)];</script>
阅读(7) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
【智能机器人开发全流程:硬件选型、软件架构与ROS实战,打造高效机器人系统】 通过模块化设计与ROS框架的灵活扩展,可快速搭建适应不同场景的机器人系统。实际开发中需重点关注。 阅读详情

相关推荐

python使用TensorRT报错,多线程或多进程中使用问题

python使用TensorRT报错,Cuda Runtime问题

qq_44224801的博客 747

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

量化策略缺数据?一份QMT/PTrade免费数据清单与特殊数据源QtFactor评测

本文为量化策略开发者提供QMT和PTrade平台的免费数据资源清单,并深度评测QtFactor在补充特色数据方面的价值。涵盖Tick级数据、Level2行情、资金流向等关键信息,帮助开发者在预算有限的情况下构建高效数据解决方案,特别适合打板策略和日内回转交易。

weixin_29058331的博客 415

[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

一文带你完成ARMv8架构中的cache知识点

【摘要】 在最初开发ARM架构时,处理器的时钟速度和内存的访问速度大致相同。今天的处理器内核要复杂得多,其时钟速度可以快上几个数量级。但是,外部总线和内存设备的频率并没有扩大到同样的程度。有可能实现小块的片上SRAM,它可以以与内核相同的速度运行,但是与标准的DRAM块相比,这种RAM非常昂贵,因为后者的容量可以达到数千倍。在许多基于ARM处理器的系统中,访问外部存储器需要几十甚至几百个内核周期。 cache的技术背景 在最初开发ARM架构时,处理器的时钟速度和内存的访问速度大致相同。今天的处.

ABCCloud的博客 1854

利用恒源云云端GPU高效训练YOLOv11模型全流程:从环境搭建到模型部署,让你摆脱硬件束缚

记住,云端GPU的核心优势是“弹性算力+一键环境”,只要把握好这一工具特性,再结合YOLOv11的高性能模型架构,你也能在计算机视觉领域快速落地项目。接下来,我们从工具准备到模型部署,一步步带你实现高效训练,真正帮你解决资源与效率的核心痛点。凭借其在目标检测、实例分割任务上的卓越性能,成为众多开发者的首选。据统计,使用云端GPU训练YOLOv11,在处理百万级数据集时,训练效率较本地普通GPU提升。在安防监控中,YOLOv11可实时检测人员、车辆异常行为,某园区采用该方案后,异常事件识别准确率达。

LXY_LOVE36的博客 213

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

ComfyUI报错ModuleNotFoundError: No module named ‘comfy.options‘

好家伙,我一想我刚刚好像用了pip安装了comfy模块,不会是和ComfyUI自带的comfy模块产生冲突了吧。(3)还有的说是项目不是最新版,叫更新到最新版本的,好家伙,我报错之前根本没更新也没错,突然就报错的。(4)还有的说没有comfy.options这部分代码,叫自己去拉一个,我一看也有啊。我也不知道啥时候脑抽风去装这么个东西的,幸好我聪明机智,一眼就知道了。看把我气的,然后网上搜集各种资料,一个一个去试。

满目星辰 5158

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

Cadence PSpice 模型1:基于自带库新建二极管的PSpice模型图文教程

使用Cadence PSpice进行电路图的仿真时,难免会遇到软件自带库中没有合适的元器件的情况,这就需要设计师自己新建元器件模型,本文详细介绍使用PSpice软件修改自带库的元器件模型的参数以得到自己需要的PSpice模型库的方法。

fydar的博客 4413

MISRA C 详解 · 专栏06:2023 与 2025 更新总览(相对 2012 的差异)

本专栏 01~05 篇基于(143 规则 + 16 指令)。这六年间标准又前进了两步——(大改)和(微调)。本文补充说明「相对 2012,标准变在哪」,并给出新增规范的代表性示例。

XavierKong的博客 73

Leetcode 每日一题:Binary Tree Maximum Path Sum

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

2401_85421495的博客 1093

【扣子AI开发】如何开发一个图像生成智能体?

【扣子AI开发】如何开发一个图像生成智能体,从创建到发布

tl437002770的博客 2427

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

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.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:

iam_lain的博客 591

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

【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

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] 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

LeetCode_Binary Tree Maximum Path Sum 解题思路

小胜爱捣鼓 1093

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 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

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 Binary Tree Maximum Path Sum

求一棵二叉树中最大的路径和。该路径可以是二叉树中某一节点到树中任意一个节点的所经过的路径,不允许重复经过一个节点,不必经过根节点。

DRFish 1083

【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
上一篇: Leetcode---Path Sum
下一篇: ARM的中断和时钟中断
我把葡萄酿成酒
博客等级 码龄12年 275粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值