Leetcode first _ Binary Tree Maximum Path Sum

Source: Internet
Author: User

This is a good question.

The question specifies that the starting point and focus of the path are arbitrary. A node can be a path that contains the parent node and the left and right subtree. The key to the problem is that the left and right subtree does not return the largest sum of the entire tree, but only contains a left or right path of the Child root node. I don't know if I understand this. It is too troublesome to draw pictures ..

In this case, although the question has no restrictions on the path conditions, the path is still required. If the left and right subtree results are directly returned, the results may come from a leaf node, it may also be the entire subtree, and everything is possible. When the result is actually calculated by the root node that does not contain the subtree, it is connected to the previous layer, with this result, the node that actually contributes the result does not eventually form a path, so it is wrong. In another case, the result of the subtree uses the root node of the subtree, but the left and right grandchildren of the subtree are also used. This is not acceptable because it is connected to the upper layer, it is like a tree chart, and it is not a path.

So, what exactly will be returned? There are three options: first, return the root node of the subtree. It can be a path to the upper layer. Second, this root node and Its left subtree form the left path. 3. The following node and Its right path must contain the following node and cannot be split. This makes sense.

Another question: Do you want to know if a subtree is okay? It doesn't matter. We use sequential traversal. We have already considered the automatic prototyping of our ancestors. It's amazing that the final update results can be considered separately from the sub-problem responses.

The ac code is as follows, which is concise:

int res;int tpMaxSum(TreeNode *root){    if(root == NULL)        return 0;    int mval = 0, lval = 0, rval = 0;    mval = root->val;    if(root->left)        lval = tpMaxSum(root->left);    if(root->right)        rval = tpMaxSum(root->right);    if(lval>0)  mval += lval;    if(rval>0)  mval += rval;    if(mval>res)    res = mval;    return max(root->val, max(root->val+lval, root->val+rval));}    class Solution {public:    int maxPathSum(TreeNode *root) {        if(root == NULL)            return 0;        res = 0xc0000000;        tpMaxSum(root);        return res;    }};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.