LeetCode 热题 HOT 100 (025/100)【宇宙最简单版】

【二叉树】No. 0124 二叉树中的最大路径和 【困难】👉力扣对应题目指路

希望对你有帮助呀!!💜💜 如有更好理解的思路,欢迎大家留言补充 ~ 一起加油叭 💦

欢迎关注、订阅专栏 【力扣详解】谢谢你的支持!

题目描述:二叉树中的 路径 被定义为一条节点序列,序列中每对相邻节点之间都存在一条边。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个 节点,且不一定经过根节点。

  • 路径和 是路径中各节点值的总和

  • 示例:

    输入:root = [-10,9,20,null,null,15,7]

    输出:42

    解释:最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42

🔥 思路 :需要结合左、右孩子的情况确定以当前节点为中间点时的最优路径和,所以采用 后续遍历

  • 当前节点 node 为中间点时的最优路径和由 node.val + left_result + right_result 计算获得
    • left_resultright_result 计算时仅能选择其左、右孩子中的一个

⭐题目准备之后续遍历:一定要先掌握后续遍历 ❗❗❗ 放在最后面啦,供不熟悉的友友参考~


参考如上思路,给出详细步骤如下:

  • 步骤一⭐确定递归函数 traversal 参数及返回值
    • 参数:需要根据当前节点 current,...
      • 计算当前节点 node 为中间点时的最优路径和 temp_max = node.val + left_result + right_result 【💥 重要】
        • 漏掉这一步的话,会误解如【本文开头示例】所示的情况
      • 计算当前节点 node 为单侧中继点时的部分最优路径和 node.val + max(left_result, right_result) 💡
    • 返回值:当前节点 node 为单侧中继点时的部分最优路径和 💡
  • 步骤二⭐确定递归终止条件: 走到了 None 节点
  • 步骤三⭐确定单层递归逻辑-后序处理:根据左、右子树的递归返回值情况,确定当前节点的返回值
python3 复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def __init__(self):
        self.max = -1000
    
    def maxPathSum(self, root: Optional[TreeNode]) -> int:
        def traversal(node):  # ------------------------------------- step 1
            if node == None: return 0  # ---------------------------- step 2
			# ------------------------------------------------------- step 3
            left_result     = max(traversal(node.left),0)
            right_result    = max(traversal(node.right),0)

            temp_max = node.val + left_result + right_result  ## 【💥 重要】
            self.max = max(self.max, temp_max)

            return node.val + max(left_result, right_result)  ## 💡

        traversal(root)
        return self.max

⭐⭐⭐ 题目准备之后续遍历:一定要先掌握后续遍历 ❗❗❗

python3 复制代码
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        self.result = []

        def traversal(current):
            if current == None:
                return
            print('-------------------------Hi, node: ', current.val)
            traversal(current.left)
            traversal(current.right)
            print('----- current_val: ', current.val)  // 观察此处的处理顺序,是后序
            self.result.append(current.val)
        
        traversal(root)  ## self.result: [6, 7, 4, 2, 5, 0, 8, 1, 3]
  • 对应的输出结果如下:

    shell 复制代码
    ('-------------------------Hi, node: ', 3)
    ('-------------------------Hi, node: ', 5)
    ('-------------------------Hi, node: ', 6)
    ('----- current_val: ', 6)
    ('-------------------------Hi, node: ', 2)
    ('-------------------------Hi, node: ', 7)
    ('----- current_val: ', 7)
    ('-------------------------Hi, node: ', 4)
    ('----- current_val: ', 4)
    ('----- current_val: ', 2)
    ('----- current_val: ', 5)
    ('-------------------------Hi, node: ', 1)
    ('-------------------------Hi, node: ', 0)
    ('----- current_val: ', 0)
    ('-------------------------Hi, node: ', 8)
    ('----- current_val: ', 8)
    ('----- current_val: ', 1)
    ('----- current_val: ', 3)

希望对你有帮助呀!!💜💜 如有更好理解的思路,欢迎大家留言补充 ~ 一起加油叭 💦
🔥 LeetCode 热题 HOT 100

相关推荐
StickToForever几秒前
第4章 信息系统架构(二)
经验分享·笔记·学习·职场和发展
查理零世26 分钟前
【蓝桥杯集训·每日一题2025】 AcWing 6134. 哞叫时间II python
python·算法·蓝桥杯
仟濹26 分钟前
【二分搜索 C/C++】洛谷 P1873 EKO / 砍树
c语言·c++·算法
紫雾凌寒35 分钟前
解锁机器学习核心算法|神经网络:AI 领域的 “超级引擎”
人工智能·python·神经网络·算法·机器学习·卷积神经网络
京东零售技术1 小时前
AI Agent实战:打造京东广告主的超级助手 | 京东零售技术实践
算法
且听风吟ayan2 小时前
leetcode day19 844+977
leetcode·c#
MiyamiKK572 小时前
leetcode_位运算 190.颠倒二进制位
python·算法·leetcode
C137的本贾尼2 小时前
解决 LeetCode 串联所有单词的子串问题
算法·leetcode·c#
青橘MATLAB学习2 小时前
时间序列预测实战:指数平滑法详解与MATLAB实现
人工智能·算法·机器学习·matlab
lingllllove2 小时前
matlab二维艾里光束,阵列艾里光束,可改变光束直径以及距离
开发语言·算法·matlab