Leetcode 124. Binary Tree Maximum Path Sum

原创 2016年08月31日 12:50:39

题意:跟一个二叉树,求最大路径和。(路径和被定义为,从一个节点到另一个节点的值得和,注意:不一定是叶子节点)


思路:

1,最长路径和,必定是以某个节点为根,以此节点左子树的根到某个节点的最大路径,以此节点右子树的根到某节点的最大路径,的和。

2.对于一个节点,只存在3种情况,此节点是最长路径和的根,最长路径和的根在此节点的左子树,最长路径和的根在此节点的右子树

3.可以用递归解决,用maxpathsum变量存最大路径和。用onepath存从根为某个节点的最大路径,

onepath = max(root->left:onepath,root->right:onepath,0) + root->val; 

maxpathsum = max(onepath,maxpathsum);

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxPathSum(TreeNode* root) {
        if(root == NULL)
            return 0;
        pathmax = (1<<31);
        MaxPathSum(root);
        
        return pathmax;
    }
    int MaxPathSum(TreeNode* root){
        if(root == NULL)
            return 0;
        
        int onepath,lpath,rpath,maxsum;
        //onepath根到某节点的最大单项路径和
        //lpath,rpath,子树的onepath
        //maxsum以root为根的最大路径和
        
        lpath = MaxPathSum(root->left);
        rpath = MaxPathSum(root->right);
        
        if(lpath<0 && rpath<0)
            onepath = root->val;
        else 
            onepath = (lpath>rpath?lpath:rpath) + root->val;
        
        maxsum = root->val;//
        if(lpath > 0)
            maxsum += lpath;
        if(rpath > 0)
            maxsum += rpath;
        cout<< onepath<<endl;
        pathmax = pathmax>(maxsum)?pathmax:maxsum;
        return onepath;
    }
    int pathmax;
    
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode(124) 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 ...

Leetcode 124 Binary Tree Maximum Path Sum 二叉树最大路径和

给出一棵二叉树,计算其最大路径和。路径的起止结点必须位于树内。

Leetcode 124. Binary Tree Maximum Path Sum[hard]

题目: Given a binary tree, find the maximum path sum.For this problem, a path is defined as any seque...

leetcode-124:Binary Tree Maximum Path Sum(Java)

Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may st...

leetcode解题之124. Binary Tree Maximum Path Sum Java版 (二叉树的最大路径和)

124. Binary Tree Maximum Path Sum ,leetcode解题之124. Binary Tree Maximum Path Sum Java版 (二叉树的最大路径和)

[leetcode]124. Binary Tree Maximum Path Sum@Java解题报告

https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/ Given a binary tree, ...

Leetcode #124 Binary Tree Maximum Path Sum

Binary Tree Maximum Path Sum题目描述 给出一颗二叉树,每个树节点有一个整数权值。 现在要求从树上找出一条路径,使得权值之和最大,问这个最大权值之和是多少。算法分析 一...

[leetcode] 124. 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 tr...

[LeetCode]*124.Binary Tree Maximum Path Sum

【题目】 Given a binary tree, find the maximum path sum. The path may start and end at any node in th...

LeetCode: Binary Tree Maximum Path Sum [124]

【题目】 Given a binary tree, find the maximum path sum. The path may start and end at any node in the...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)