LeetCode Binary Tree Maximum Path Sum

class Solution {
public:
    int maxPathSum(TreeNode *root) {
        int s, m;
        dfs(root, s, m);
        return (m > s) ? m : s;
    }
    
    void dfs(TreeNode* root, int& sole, int& misc) {
        if (root == NULL) {
            sole = INT_MIN;
            misc = INT_MIN;
            return;
        }
        int ls, lm, rs, rm, ms, mm;
        dfs(root->left, ls, lm);
        dfs(root->right, rs, rm);
        
        ms = (ls > rs) ? ls : rs;
        mm = (lm > rm) ? lm : rm;
        
        sole = root->val + (ms < 0 ? 0 : ms);
        int m = ((ls < 0 ? 0 : ls) + (rs < 0 ? 0 : rs) + root->val);
        misc = mm > m ? mm : m;
    }
};

dfs深度优先遍历计算路径和,它分别计算经过该节点,且另一个端点在左或右子树中的最大路径和(得到的这两个路径是不会跨越该节点的,用sole表示),那么对于那些端点不包括当前节点,分布在左/右子树中的(该种类的)最大路径和用misc表示。则以当前节点为根的子树的最大路径和,可能是其某个子树中的misc类路径,也可能是左sole+右sole+当前节形成的路径,通过比较他们两者的值我们可以得到该子树的最大路径和。同样的对于整棵树也是如此。

可以看到misc变量只负责存储dfs搜索过程中的最大路径和,因此也可以把包含左右子树中路径的最大路径和存到一个全局/类变量中,并初始化为最小值。

class Solution {
private:
    int max_path;
public:
    int maxPathSum(TreeNode *root) {
        max_path = INT_MIN;
        dfs(root);
        return max_path;
    }

    int dfs(TreeNode* root) {
        if (root == NULL) return 0;
        int ls = dfs(root->left);
        int rs = dfs(root->right);
        
        int ms = (ls > rs) ? ls : rs;
        
        int sole = root->val + (ms < 0 ? 0 : ms);
        
        int m = ((ls < 0 ? 0 : ls) + (rs < 0 ? 0 : rs) + root->val);
        
        if (m > max_path) max_path = m;
        
        return sole;
    }

};

 

参考:

zhuli题解 http://www.cnblogs.com/zhuli19901106/p/3547387.html

第二轮:

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.

 

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    int maxsum;
public:
    int maxPathSum(TreeNode *root) {
        maxsum = INT_MIN;
        dfs(root);
        return maxsum;
    }
    
    int dfs(TreeNode* root) {
        if (root == NULL) return 0;
        int val = root->val;
        int mvl = dfs(root->left);
        int mvr = dfs(root->right);
        int s = val;
        if (mvl > 0) s+=mvl;
        if (mvr > 0) s+=mvr;
        if (s > maxsum) {
            maxsum = s;
        }
        int mv = max(mvl, mvr);
        return val + (mv > 0 ? mv : 0);
    }
};

 再一次,这回可以自己想出来了,记忆的关系吧:

/**
 * 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 {
private:
    int maxsum;
public:
    int maxPathSum(TreeNode* root) {
        maxsum = INT_MIN;
        dfs(root);
        return maxsum;
    }
    
    int dfs(TreeNode* root) {
        if (root == NULL) {
            return 0;
        }
        int ld = dfs(root->left);
        int rd = dfs(root->right);
        int csum = root->val;
        if (ld > 0) {
            csum += ld;
        }
        if (rd > 0) {
            csum += rd;
        }
        maxsum = max(maxsum, csum);
        
        return (max(ld, rd) > 0 ? max(ld, rd) : 0) + root->val;
    }
};

 

posted @ 2014-03-22 01:26  卖程序的小歪  阅读(270)  评论(0编辑  收藏  举报