11Leetcode---二叉树最大路径和

本文深入探讨了寻找二叉树中最大路径和的算法实现,通过四种可能的路径情况分析,提供了两种不同语言的代码示例,即C++和Java。文章详细解释了递归函数的逻辑,包括如何计算以当前节点为根的最大路径和,以及如何更新全局最大值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目: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.

思路:首先我们分析一下对于指定某个节点为根时,最大的路径和有可能是哪些情况。一共有4中情况,第一种左子树的路径加上当前节点;第二种,右子树的路径加上当前节点;第三种,左右子树的路径加上当前节点(相当于一条横跨当前节点的路径);第四种,只有自己的路径。这四种情况只是用来计算以当前节点根的最大路径,如果当前节点上面还有节点,那它的父节点是不能累加第三种情况的,所以我们要计算两个最大值,一个是当前节点下最大路径和,另一个是如果要连接父节点时最大的路径和。我们用前者更新全局最大量,用后者返回递归值就行了。

其中左右子树的最大路径和:贪心来做,状态转移方程

maxSum(*root)=max(root->val, root->val+max(maxSum(*root->left), maxSum(*root->right))).

代码1:
class Solution {
public:
    int help(TreeNode* root, int &re){//以该节点为根节点的最大路径和
        if (!root) return 0;
        int l=max(0, help(root->left, re));
        int r=max(0, help(root->right, re));
       //当前节点的最大路径是一、二、三、四这四种情况的最大值
        re = max(re, root->val+l+r);
        //连接父节点的最大路径是一、二、四这三种情况的最大值
        return max(root->val, root->val+max(l, r));
    }
    int re;
    int maxPathSum(TreeNode* root) {
        int re=INT_MIN;
        help(root, re);
        return re;
    }
};
代码2:
public class Solution {
    private int max = Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        helper(root);
        return max;
    }   
    public int helper(TreeNode root) {
        if(root == null) return 0;
        int left = helper(root.left);
        int right = helper(root.right);
        //连接父节点的最大路径是一、二、四这三种情况的最大值
        int currSum = Math.max(Math.max(left + root.val, right + root.val), root.val);
        //当前节点的最大路径是一、二、三、四这四种情况的最大值
        int currMax = Math.max(currSum, left + right + root.val);
        //用当前最大来更新全局最大
        max = Math.max(currMax, max);
        return currSum;
    }
}

reference:https://blog.youkuaiyun.com/mmwwxx123/article/details/81867450

                   https://www.cnblogs.com/junliu37/p/7224172.html 

                   https://www.cnblogs.com/junliu37/p/7224172.html

### LeetCode 298 二叉树最长连续序列 对于LeetCode上的编号为298的题目“二叉树中最长的连续序列”,目标是在给定的二叉树中找到最长的连续递增路径长度。这里的连续意味着节点值依次增加1。 #### 解决方案概述 解决方案涉及深度优先搜索(DFS)遍历整棵,同时跟踪当前路径是否构成连续递增序列以及该序列的长度。当遇到不满足条件的情况时,则重置计数器并继续探索其他分支[^4]。 #### Python代码实现 下面是一个基于上述思路的具体Python实现: ```python class Solution(object): def longestConsecutive(self, root): """ :type root: TreeNode :rtype: int """ def dfs(node, parent_val, cur_length): if not node: return # 如果当前节点值正好是父节点值加一,则认为找到了一个新的连续部分 if node.val == parent_val + 1: nonlocal max_length max_length = max(max_length, cur_length + 1) # 继续向下层传递更新后的参数 dfs(node.left, node.val, cur_length + 1) dfs(node.right, node.val, cur_length + 1) else: # 否则重新开始计算新的潜在连续序列 dfs(node.left, node.val, 1) dfs(node.right, node.val, 1) max_length = 0 # 初始化调用栈 dfs(root, float('-inf'), 0) return max_length ``` 此方法通过递归方式访问每一个节点,并利用`parent_val``cur_length`两个额外参数来帮助判断是否存在连续关系及其对应的长度变化情况。最终结果保存于全局变量`max_length`之中,在完成整个形结构扫描之后返回作为答案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值