leetcode: Binary Tree Maximum Path Sum

504 篇文章 0 订阅
230 篇文章 0 订阅

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.

this code is a little bit complicated, I will revise it soon

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
#define INVALID_NUM 0xFFFFFFFF
 
class Solution {
    
    int maxPathSum2(TreeNode *root, int &maxVal)
    {
        if (root == NULL)
        {
            return INVALID_NUM;
        }
        
        int leftMax  = maxPathSum2(root->left, maxVal);
        int rightMax = maxPathSum2(root->right, maxVal);   
        int curMax;
        
        
        if (leftMax != INVALID_NUM && rightMax != INVALID_NUM)
        {
            if (leftMax <= 0 && rightMax <=0)
            {
                curMax = root->val;
            }
            else if (leftMax <= 0)
            {
                curMax = root->val+rightMax;
            }
            else if (rightMax <= 0)
            {
                curMax = root->val+leftMax;
            }
            else
            {
                if (root->val+leftMax >= root->val+rightMax)
                {
                    curMax = root->val+leftMax;
                }
                else
                {
                    curMax = root->val+rightMax;
                }
    
                if (root->val >= curMax)
                    curMax = root->val;
                    
                if (root->val+leftMax+rightMax > maxVal)
                {
                    maxVal = root->val+leftMax+rightMax;
                }
                return curMax;
            }
               
        }
        else if (leftMax != INVALID_NUM)
        {
            if (leftMax <= 0)
            {
                curMax = root->val;
            }
            else
            {
                curMax = root->val+leftMax;
            }
        }
        else if (rightMax != INVALID_NUM)
        {
            if (rightMax <= 0)
            {
                curMax = root->val;
            }
            else
            {
                curMax = root->val+rightMax;
            }
        }
        else
        {
            curMax = root->val;
        }
        
        if (curMax > maxVal)
            maxVal = curMax;

        return curMax;
    }
    
public:
    int maxPathSum(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() functionr
        int maxVal = -10000;
        maxPathSum2(root, maxVal);
        
        return maxVal;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值