马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
【二叉树】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_result 和 right_result 盘算时仅能选择其左、右孩子中的一个
⭐标题准备之后续遍历:肯定要先把握后续遍历 ❗❗❗ 放在最反面啦,供不认识的友友参考~
参考如上思绪,给出具体步调如下:
- 步调一⭐确定递归函数 traversal 参数及返回值
- 参数:须要根据当前节点 current,…
- 盘算当前节点 node 为中心点时的最优路径和 temp_max = node.val + left_result + right_result 【💥 告急】
- 遗漏这一步的话,会误解如【本文开头示例】所示的环境
- 盘算当前节点 node 为单侧中继点时的部门最优路径和 node.val + max(left_result, right_result) 💡
- 返回值:当前节点 node 为单侧中继点时的部门最优路径和 💡
- 步调二⭐确定递归停止条件: 走到了 None 节点
- 步调三⭐确定单层递归逻辑-后序处置处罚:根据左、右子树的递归返回值环境,确定当前节点的返回值
- # 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
复制代码 ⭐⭐⭐ 标题准备之后续遍历:肯定要先把握后续遍历 ❗❗❗
- # 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]
复制代码
- 对应的输出效果如下:
- ('-------------------------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
|