binary-tree-maximum-path-sum

来源:互联网 发布:淘宝怎么做详情页 编辑:程序博客网 时间:2024/06/05 06:55

题目:

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

Return6.

程序:

class Solution {public:     int Max;    int maxPathSum(TreeNode *root) {        Max = INT_MIN;        maxSum(root);         return Max;    }    int maxSum(TreeNode *root){        if(root == NULL)            return 0;        int l_Max = max(0, maxSum(root->left));        int r_Max = max(0, maxSum(root->right));        Max = max(Max, l_Max + r_Max + root->val);        return max(l_Max, r_Max) + root->val;     }};
原创粉丝点击