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

相关推荐
我爱C编程2 小时前
基于Qlearning强化学习的1DoF机械臂运动控制系统matlab仿真
算法
chao_7892 小时前
CSS表达式——下篇【selenium】
css·python·selenium·算法
chao_7892 小时前
Selenium 自动化实战技巧【selenium】
自动化测试·selenium·算法·自动化
YuTaoShao2 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
怀旧,2 小时前
【数据结构】8. 二叉树
c语言·数据结构·算法
泛舟起晶浪2 小时前
相对成功与相对失败--dp
算法·动态规划·图论
地平线开发者3 小时前
地平线走进武汉理工,共建智能驾驶繁荣生态
算法·自动驾驶
IRevers3 小时前
【自动驾驶】经典LSS算法解析——深度估计
人工智能·python·深度学习·算法·机器学习·自动驾驶
前端拿破轮3 小时前
翻转字符串里的单词,难点不是翻转,而是正则表达式?💩💩💩
算法·leetcode·面试
凤年徐3 小时前
【数据结构与算法】203.移除链表元素(LeetCode)图文详解
c语言·开发语言·数据结构·算法·leetcode·链表·刷题