LeetCode | Binary Tree Maximum Path Sum

题目:

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

思路:

思路1:首先需要递归地计算某个节点左子树、右子树的路径和,便能得到每个某个节点为路径上的一个点的路径和。遍历整棵树便能得到最大路径和。思路2:思路1的时间复杂度太高,因此我们可以定义一个公共变量,在计算子树最大路径的时候就比较子树的path sum。

代码:

思路1:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxPathSum(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        
        if(root == NULL)
        {
            return INT_MIN;
        }
        else
        {
            int l = maxPath(root->left);
            int r = maxPath(root->right);
            int sum = 0;
            if(l>0)
            {
                sum += l;
            }
            if(r>0)
            {
                sum += r;
            }
            sum += root->val;
            
            int lm = maxPathSum(root->left);
            int rm = maxPathSum(root->right);
            
            int max = INT_MIN;
            if(sum>max)
                max = sum;
            if(lm>max)
                max = lm;
            if(rm>max)
                max = rm;
            return max;
        }
    }
    
    int maxPath(TreeNode *root)
    {
        if(root == NULL)
        {
            return INT_MIN;
        }
        else if(root->left == NULL)
        {
            
            int max = maxPath(root->right);
            return max<0?root->val:max+root->val;
        }
        else if(root->right == NULL)
        {
            int max = maxPath(root->left);
            return max<0?root->val:max+root->val;
        }
        else
        {
            int max = 0;
            int lmax = maxPath(root->left);
            int rmax = maxPath(root->right);
            max = (lmax<rmax?rmax:lmax);
            max = (max<0?root->val:max+root->val);
            return max;
        }
    }
};

思路2:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxPS;
    int maxPathSum(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        maxPS = INT_MIN;
        maxPath(root);
        return maxPS;
    }
    
    int maxPath(TreeNode *root)
    {
        int r = INT_MIN;
        if(root == NULL)
        {
        }
        else if(root->left == NULL)
        {
            int max = maxPath(root->right);
            r = max<0?root->val:max+root->val;
        }
        else if(root->right == NULL)
        {
            int max = maxPath(root->left);
            r = max<0?root->val:max+root->val;
        }
        else
        {
            int max = INT_MIN;
            int lmax = maxPath(root->left);
            int rmax = maxPath(root->right);
            max = (lmax<rmax?rmax:lmax);
            max = (max<0?root->val:max+root->val);
            if((lmax+rmax+root->val)>maxPS)
            {
                maxPS = (lmax+rmax+root->val);
            }
            r = max;
        }
        if(r>maxPS)
        {
            maxPS = r;
        }
        return r;
    }
};

    原文作者:Allanxl
    原文地址: https://blog.csdn.net/lanxu_yy/article/details/11880189
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞