leetcode#124. The maximum path sum in a binary tree

Topic link

Title description:

Given a non-empty binary tree, return its maximum path sum.

In this question, the path is defined as a sequence starting from any node in the tree and reaching any node. The path contains at least one node and does not necessarily pass through the root node.

Examples:
leetcode

Problem-solving ideas

  This question belongs to leetcode hard difficulty. I think the main difficulty lies in whether you really understand the meaning of the question . The rest is practice makes perfect ~ In addition, this question is in the same line as the height and path of the tree, and you can organize it yourself if you have time ~ mutual encouragement!
  The maximum path defined by the title is any path in the tree , and it does not have to go through the root node. With examples , for example, 9,-10, 15,20,7, 9,-10,20,15can be a path, but 9,-10,20,15,7not the path. In fact, a major feature of this kind of tree structure composed of a singly linked list is that it can only be traversed from top to bottom and cannot be returned. For example, the parent node can access its corresponding child node, but its child node wants to go back. It's not working. At this point, some people will wonder whether this question just needs the child node to " return " to the parent node, which is what we often call backtracking . Let's talk about why we need to use retrospective thinking.
  Retrospective thinking is nothing more than "all roads lead to Rome, if this road fails, then change the other road", the focus is on how to save the information we need from the previous road and apply it to the next road, in short, state reset. Far away ~ back to this question, since any path in the tree that conforms to the parent-child structure may be the maximum sum of the path, then suppose we have traversed to a certain node. If the maximum sum of the path includes the current node, let's take a look. The possible maximum sum:

  1. Current node + maximum sum of the left subtree of the current node + maximum sum of the right subtree of the current node
  2. The current node + the maximum sum of the left subtree of the current node + the parent node of the current node (recursively considered, it can be the parent node of the parent node...) + the maximum sum of another subtree of the determined parent node
  3. The current node + the maximum sum of the right subtree of the current node + the parent node of the current node (recursively considered, it can be the parent node of the parent node...) + the maximum sum of another subtree of the determined parent node

  From the above three situations, we can conclude that it 1belongs to conventional thinking and does not involve its parent node. In fact, it is also the idea of ​​finding the height of the tree and the path of the leaves. But 2、3it's not the same. We will find that in these two cases there will be operations where the child node passes the state back to the parent node, which is why we need to use the idea of backtracking !
  After the above situation discussion, we need to traverse from the bottom to the top to obtain the results, which will use the post-order traversal recursive framework, the main core is the processing of the current node, that is, the processing of the three situations , the rest of the interaction Do it recursively.
  For these three situations, we can divide the discussion into two parts- 1and 2、3. For 1, we only need root->val + leftMax + rightMaxto update the maximum path sum after the sum ( ) maxSum. For 2、3, what we have to do is how to save the current state and pass it to the upper layer of nodes. You can set a variable tempto get the maximum path sum through the current node, and then return to the upper layer. Some attention after the current node's path and the maximum 1difference between the largest and the path of figuring out this distinction abnormal key . Take an example: we are now operating the current node 20, which is obviously 15,20,7a situation 1, but 15,20a situation 2、3, and 15,20will return to the parent node to -10make judgments.
  In additionIn order to improve efficiency, we found negative for seeking the path of the largest and unfavorable, so in demand leftMax, rightMax, tempneed to add a judgment, if it is negative, decisively abandon.


Code

在这里插入代码片class Solution {
    
    
public:
    int maxPathSum(TreeNode* root) {
    
    
        if(!root)
            return 0;
        
        helper(root);
        return maxSum;
    }

    int helper(TreeNode* root){
    
    
        if(!root)
            return 0;
        //1 属于后序遍历框架中对当前结点的操作,只需实时更新最大和即可
        //2.3 含有回溯思想,由于遍历是自顶向下的过程,需要通过某种方式将当前结点的状态(当前结点+当前结点的左/右子树最大和)返回到其父结点
        当前结点状态为负,抛弃
        int leftMax = max(0, helper(root->left));        //获取当前结点左子树的最大路径和
        int rightMax = max(0, helper(root->right));      //获取当前结点右子树的最大路径和
        //获取以当前结点为父节点的最大路径和
        maxSum = max(maxSum, root->val + leftMax + rightMax);
        //获取经过当前结点且可以回到上一层的最大路径和  
        int temp = max(0, max(leftMax, rightMax) + root->val);

        return temp;      
    }

    
private:
    int maxSum = INT_MIN;
};

If there are mistakes or not rigorous, please correct me, thank you very much.
My blog: http://breadhunter.gitee.io

Guess you like

Origin blog.csdn.net/weixin_40807714/article/details/104909893