LeetCode Algorithm Recursive Class - Maximum Path Sum in Binary Tree

Table of contents

124. Maximum Path Sum in Binary Tree - LeetCode

answer:

code:

operation result:


 A path in a binary tree  is defined as a sequence of nodes with an edge between every pair of adjacent nodes in the sequence. The same node can appear at most once in a path sequence   . The path  contains at least one  node and does not necessarily go through the root node.

The path sum  is the sum of the values ​​of each node in the path.

Given the root node of a binary tree  root , return its  maximum path sum  .

Example 1:

Input: root = [1,2,3]
 Output: 6
 Explanation: The optimal path is 2 -> 1 -> 3, and the path sum is 2 + 1 + 3 = 6

Example 2:

Input: root = [-10,9,20,null,null,15,7]
 Output: 42
 Explanation: The optimal path is 15 -> 20 -> 7, and the path sum is 15 + 20 + 7 = 42

hint:

  • The number of nodes in the tree ranges from [1, 3 * 104]
  • -1000 <= Node.val <= 1000

题解:

This question requires the calculation of the path and the maximum path sum in the binary tree, that is, the maximum value of the sum of paths starting from any node, passing through any node, and arriving at any node.

To solve this problem, we can traverse the binary tree recursively. For each node, we need to calculate the maximum contribution value of the node, that is, the maximum sum that the node can provide when it is part of the path.

Specific steps are as follows:

  1. Create a variable  maxSumto store the result, initially set to negative infinity ( Integer.MIN_VALUE).
  2. Define a recursive function  gain(TreeNode root)that calculates the maximum contribution value of a node and updates the value during the recursion  maxSum .
  3. In the recursive function, the termination condition is judged first, and if the current node is empty, 0 is returned directly.
  4. Recursively calculate the maximum contribution value of the left child node and the right child node respectively, and take a larger value between it and 0. This is because the gain to the path sum is 0 when the contribution value is negative.
  5. Calculate the maximum path sum of the current node, that is, the sum of the node value plus the maximum contribution value of the left and right child nodes.
  6. Compare the maximum path sum of the current node with  maxSum and take the larger value to update  maxSum.
  7. Returns the maximum contribution value of the current node, which is the larger value between the node value plus the maximum contribution value of the left and right child nodes.
  8. In  maxPathSum the function, call  gain the function to calculate the maximum contribution value of the root node, and return  maxSum the value as the result.

In this way, when the recursion ends, maxSumwhat is stored in is the path sum in the binary tree and the maximum path sum.

代码:

class Solution {
    // 存储结果
    int maxSum =Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        gain(root);
        return maxSum;
    }
    // 计算节点最大贡献值
    public int gain(TreeNode root){
        // 终止条件
        if(root==null) return 0;
        // 递归计算左右子节点最大贡献值(结果为负数不计入)
        int left=Math.max(gain(root.left),0);
        int right=Math.max(gain(root.right),0);

        // 计算该结点的最大路径和
        // 节点最大路径和为该节点的值与该节点左右子节点的最大贡献值
        int max=root.val+left+right;
        //比较该节点的最大路径和与目前值(贪心算法)
        maxSum=Math.max(maxSum,max);

        // 返回该节点最大贡献值
        return root.val+Math.max(left,right);
    }
}

运行结果:

 

Guess you like

Origin blog.csdn.net/qq_62799214/article/details/132384429